Rails 4 - 简单形式参数
Rails 4 - Simple form arguments
谁能看出这种表达方式有什么问题?
<%= simple_fields_for :article do |f| %>
<%=f.input :q, :nil, placeholder: "Search" %>
<% end %>
错误消息说:
wrong number of arguments (3 for 1..2)
所以-我的更广泛的代码是:
文章#index.html.erb:
<div class="row">
<div class="col-md-10 col-md-offset-1">
<%= render 'articles/searchform' %>
</div>
<div class="col-md-10 col-md-offset-1">
<% Article.all.sort_by(&:created_at).in_groups_of(3) do |group| %>
</div>
<div class="row">
<% group.compact.each do |article| %>
<div class="col-md-4">
<div class="indexdisplay">
<%= image_tag article.image_url, width: '100%', height:
'200px' if article.image.present? %>
<div class="indexheading"> <%= link_to article.title, article %> </div>
<div class="indexsubtext"> <%= truncate(article.body, :ommission =>
"...", :length => 250) %></div>
</div>
</div>
<% end %>
</div>
<div class="row">
<%= link_to 'New Article', new_article_path %>
</div>
在我看来文章#form.html.erb
<div class="col-xs-10 col-xs-offset-1">
<%= simple_form_for(@article) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title, autofocus: true %>
<%= f.input :body, :label => "Post", :input_html => {:rows => 10} %>
<%= f.input :image, :label => "Add an image" %>
</div>
<div class="form-actions">
<%= f.button :submit, "Review a draft", :class =>
'formsubmit' %>
</div>
<% end %>
在文章中#searchform.html.erb
<% f.simple_fields_for :article do |a| %>
<%=a.input :q, :nil, placeholder: "Search" %>
<% end %>
我收到这个错误:
undefined local variable or method `f' for #<#<Class:0x007f874133c410>:0x007f8740448058>
您缺少表单变量,例如f
。
例如,如果你有 simple_form_for ... do |f|
,你应该写
= f.simple_fields_for :article do |article|
= article.input :q, :nil, placeholder: 'search'
[编辑] 你看起来很困惑。 simple_fields_for
是一个特殊命令,用于在表单内迭代嵌套项(例如 post 的注释)。所以 simple_fields_for
需要一个包围形式 (simple_form_for
)。对于您的搜索表单,您应该只使用 simple_form_for
.
谁能看出这种表达方式有什么问题?
<%= simple_fields_for :article do |f| %>
<%=f.input :q, :nil, placeholder: "Search" %>
<% end %>
错误消息说:
wrong number of arguments (3 for 1..2)
所以-我的更广泛的代码是:
文章#index.html.erb:
<div class="row">
<div class="col-md-10 col-md-offset-1">
<%= render 'articles/searchform' %>
</div>
<div class="col-md-10 col-md-offset-1">
<% Article.all.sort_by(&:created_at).in_groups_of(3) do |group| %>
</div>
<div class="row">
<% group.compact.each do |article| %>
<div class="col-md-4">
<div class="indexdisplay">
<%= image_tag article.image_url, width: '100%', height:
'200px' if article.image.present? %>
<div class="indexheading"> <%= link_to article.title, article %> </div>
<div class="indexsubtext"> <%= truncate(article.body, :ommission =>
"...", :length => 250) %></div>
</div>
</div>
<% end %>
</div>
<div class="row">
<%= link_to 'New Article', new_article_path %>
</div>
在我看来文章#form.html.erb
<div class="col-xs-10 col-xs-offset-1">
<%= simple_form_for(@article) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title, autofocus: true %>
<%= f.input :body, :label => "Post", :input_html => {:rows => 10} %>
<%= f.input :image, :label => "Add an image" %>
</div>
<div class="form-actions">
<%= f.button :submit, "Review a draft", :class =>
'formsubmit' %>
</div>
<% end %>
在文章中#searchform.html.erb
<% f.simple_fields_for :article do |a| %>
<%=a.input :q, :nil, placeholder: "Search" %>
<% end %>
我收到这个错误:
undefined local variable or method `f' for #<#<Class:0x007f874133c410>:0x007f8740448058>
您缺少表单变量,例如f
。
例如,如果你有 simple_form_for ... do |f|
,你应该写
= f.simple_fields_for :article do |article|
= article.input :q, :nil, placeholder: 'search'
[编辑] 你看起来很困惑。 simple_fields_for
是一个特殊命令,用于在表单内迭代嵌套项(例如 post 的注释)。所以 simple_fields_for
需要一个包围形式 (simple_form_for
)。对于您的搜索表单,您应该只使用 simple_form_for
.