我正在努力构建一个帮助器,输出一个由集合的所有成员组成的<'ul>.对于集合中的每个成员,我想打印出一个带有标题的<'li>,以及一个指向CRUD成员的div链接.这与Rails为索引视图的脚手架输出非常相似.
这是我得到的帮手:
def display_all(collection_sym)
collection = collection_sym.to_s.capitalize.singularize.constantize.all
name = collection_sym.to_s.downcase
html = ''
html << "<ul class=\"#{name}-list\">"
for member in collection do
html << content_tag(:li, :id => member.title.gsub(' ', '-').downcase.strip) do
concat content_tag(:h1, member.title, :class => "#{name}-title")
concat link_to 'Edit', "/#{name}/#{member.id}/edit"
concat "\|"
concat link_to 'View', "/#{name}/#{member.id}"
concat "\|"
concat button_to 'Delete', "/#{name}/#{member.id}", :confirm => 'Are you sure? This cannot be undone.', :method => :delete
end
end
html << '</ul>'
return html
end
Run Code Online (Sandbox Code Playgroud)
而那输出正是我想要的.首先,如果有人认为有更好的方法可以做到这一点,请随意纠正我,我怀疑我是以低音方式做到这一点,但此刻它是我知道如何的唯一方式.
然后我尝试将链接包装在div中,如下所示:
def display_all(collection_sym)
collection = collection_sym.to_s.capitalize.singularize.constantize.all …Run Code Online (Sandbox Code Playgroud) ruby html-helper ruby-on-rails actionviewhelper ruby-on-rails-3
我希望从Ruby脚本中使用ActionView :: Helpers :: NumberHelper.我还需要什么等等?
我想要以下目录结构:
views/
app1/
users/_user.html.erb
users/index.html.erb
app2/
users/index.html.erb
shared/
users/_user.html.erb
users/index.html.erb
Run Code Online (Sandbox Code Playgroud)
在我看来,我打电话
# app1/users/index.html
<%= render :partial => "user" %>
# => /app1/users/_user.html.erb
# app2/users/index.html
<%= render :partial => "user" %>
# => /shared/users/_user.html.erb
Run Code Online (Sandbox Code Playgroud)
所以基本上,如何告诉Rails检查/ app2/users dir然后检查共享目录,然后才会引发它丢失的模板错误?
我绕过了这个(正如Senthil建议的那样,使用 File.exist?
这是我的解决方案 - 欢迎提出反馈和建议
# application_helper.rb
# Checks for a partial in views/[vertical] before checking in views/shared
def partial_or_default(path_name, options={}, &block)
path_components = path_name.split("/")
file_name = path_components.pop
vertical_file_path = File.join(vertical}, path_components, file_name)
shared_file_path = File.join("shared", path_components, file_name)
full_vertical_file_path = File.join("#{Rails.root}/app/views/", "_#{vertical_file_path}.html.erb")
attempt_file_path …Run Code Online (Sandbox Code Playgroud) ruby-on-rails partial-views actionview actionviewhelper ruby-on-rails-3
我在视图中使用'time_ago_in_words'函数,我需要在FunctionalTest中测试输出.
但测试无法看到'time_ago_in_words'辅助函数.
为了使用FunctionalTests中的这些辅助方法,我该怎么办?
我在这里错过了什么?我正在使用Rails 4.0.0并尝试新的Bootstrap 3.0.0rc1.我有一个简单的"食谱盒"应用程序,它有一个食谱模型和一个分类模型,可以在食谱上提供"类别"字段.在食谱#new视图中,我定义了以下字段:
<h1>Create New Recipe</h1>
<%= form_for @recipe, html: { class: 'form-horizontal' } do |f| %>
<fieldset>
<legend>Main Information</legend>
<div class="form-group">
<%= f.label :name, "Recipe name", class: "col-lg-2 control-label" %>
<div class="col-lg-6">
<%= f.text_field :name, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= f.label :category_id, class: "col-lg-2 control-label" %>
<div class="col-lg-6">
<%= f.collection_select :category, Category.all, :id, :name, class: "form-control", prompt: "Select a category" %>
</div>
</div>
...
Run Code Online (Sandbox Code Playgroud)
text_field帮助器呈现格式正确的标记,并带有class属性.但是,无论我如何构造select或collection_select助手,我似乎无法让Rails给我一个包含class属性的东西.上面的代码给了我这个:
<select id="recipe_category" name="recipe[category]"><option value="">Select a category</option>
...
Run Code Online (Sandbox Code Playgroud)
所以提示通过,但类attrib没有,所以它看起来像html_options哈希的一部分被识别.但是没有应用Bootstrap样式.如果我在类中使用大括号{}无关紧要:"form-control"与否.如果我在collection_select params周围使用parens,无关紧要.也可以选择帮助器.
任何人都可以建议吗?你也看到了这个吗?
我试图将作为哈希键的符号人性化
c.each_key{ |f| humanize(f.to_s)}
但由于某种原因,我得到了这样的错误
ActionView::Template::Error (undefined method 'humanize' for #<#<Class:0xb5b77a4>:0xb5b6598>)
知道这里出了什么问题吗?
我有点像Rails的新手.我正在写一个couchrest-rails应用程序,所以我没有使用activerecord这个模型.我只是想通了那意味着
form_for(@model)
Run Code Online (Sandbox Code Playgroud)
不行.我正在尝试研究如何使用form_tag - 但大多数示例都不涉及new和create操作.
这是错的:
<h1>New article</h1>
<% form_tag new_article_url(@article), :method => :post do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
Run Code Online (Sandbox Code Playgroud)
因为当我运行我的Cucumber场景时,我得到了这个:
Scenario: Create Valid Article # features/article.feature:16
Given I have no articles # features/step_definitions /article_steps.rb:8
And I am on the list of articles # features/step_definitions/webrat_steps.rb:6
/home/deploy/www/www.trackingplace.com/app/ccc/app/views/articles/new.html.erb:3: warning: multiple values for a block parameter (0 for …Run Code Online (Sandbox Code Playgroud) 如何在Rails控制台中执行image_tag方法
运行控制台$ rails c
加载助手
包括ActionView :: Helpers
执行命令
IMAGE_TAG( 'test.png')
我有一个奇怪的错误.

请帮忙!
如何包含资源管道执行上下文可访问的Rails视图助手?
一个示例用例是使用form_tag帮助器方法为表单生成标记,并使其可用于Javascript模板(如handlebars,jst等).
我使用handlebar_assets gem,但这也适用于任何erb或haml模板.
我正在使用一个音乐会巡回网站的应用程序,所有时间(公告时间,开始时间和活动开始时间)都在每个特定地点的时区本地.我将用户输入的日期/时间视为适用,并运行before_filter来设置适当的时区,以便所有内容都以UTC格式存储在数据库中.对于"新"表单以及在索引和显示操作中显示时间,完全没问题.当数据从数据库带回到视图中时,我使用in_time_zone来根据特定的场地进行调整.
唯一的问题是编辑表格.日期/时间选择以UTC显示数据.当我在网站上工作时,我会进行心理调整,但对于其他人而言,这是令人困惑的.我想做一些事情:
<%= f.datetime_select :start_datetime.in_time_zone(@event.time_zone) %>
或者,在控制器中:
def edit
@performance = Performance.find(params[:id])
@event = @performance.event
@performance.start_datetime = @performance.start_datetime.in_time_zone(@event.time_zone)
end
Run Code Online (Sandbox Code Playgroud)
然后简单地说,<%= f.datetime_select :start_datetime %>.
不幸的是,我没有找到正确的方法来做到这一点.你有什么值得尝试的想法吗?
非常感谢你.
actionviewhelper ×10
actionview ×6
ruby ×3
form-for ×1
forms ×1
helper ×1
html-helper ×1
irb ×1