如果使用Formtastic单独编辑多个记录,你怎么能做RyanB's Railscast所涵盖的内容? Formtastic不使用form_tag,RyanB的方法依赖于form_tag。
答案 0 :(得分:6)
semantic_form_for
只是form_for
的包装,因此您可以使用相同的参数。这是Ryan Bates' screencast
views/products/edit_individual.html.erb
<% semantic_form_for :update_individual_products, :url => update_individual_products_path, :method => :put do |f| %>
<% for product in @products %>
<% f.fields_for "products[]", product do |ff| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => ff %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
views/products/index.html.erb
<% semantic_form_for :edit_individual_products, :url => edit_individual_products_path do %>
<table>
<tr>
<th></th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
</tr>
<% for product in @products %>
<tr>
<td><%= check_box_tag "product_ids[]", product.id %></td>
<td><%=h product.name %></td>
<td><%=h product.category.name %></td>
<td><%= number_to_currency product.price %></td>
<td><%= link_to "Edit", edit_product_path(product) %></td>
<td><%= link_to "Destroy", product, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<p>
<%= select_tag :field, options_for_select([["All Fields", ""], ["Name", "name"], ["Price", "price"], ["Category", "category_id"], ["Discontinued", "discontinued"]]) %>
<%= submit_tag "Edit Checked" %>
</p>
<% end %>
请注意,您也可以form_for
使用formtastic
帮助者。
<强>更新强>
如果你想使用嵌套属性,它应该开箱即用,使用表单partial上的fields_for。让我们坚持使用railscast示例并说:
product.rb
has_many :commments
accepts_nested_attributes_for :comments
您可以编辑产品的_fields.html.erb上的评论,例如:
<%= f.fields_for :comments do |cf| %>
<%=render 'comments/fields', :f=>cf%>
<%end%>
并确保评论视图中有一些字段。