本文介绍了Railscast 198,但使用formtastic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么能够完成中的内容使用Formtastic单独编辑多个记录? Formtastic不使用form_tag,RyanB的方法依赖于form_tag。 semantic_form_for 仅仅是 > form_for ,所以你可以使用相同的参数。这里是的形式化版本。



views / products / edit_individual.html.erb

 <%semantic_form_for:update_individual_products,:url => update_individual_products_path,:method => :把do | f | %GT; 
>
< h2><%= h product.name%>< / h2>
<%end%>
<%end%>
< p><%= submit_tag提交%>< / p>
<%end%>

views / products / index.html.erb

 <%semantic_form_for:edit_individual_products,:url => edit_individual_products_path do%> 
< table>
< tr>
< th>< / th>
< th>名称< / th>
第< th>类别< / th>
Price< th>
< / tr>
>
< tr>
< td><%= check_box_tagproduct_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_product_path(product)%>< / td>
< td><%= link_toDestroy,product,:confirm => '你确定吗?',:method => :删除%>< / td>
< / tr>
<%end%>
< / table>
< p>
< / p>
<%end%>

请注意,您可以使用 form_for 帮助者以及 formtastic



更新

如果您也想使用嵌套属性,则应该使用表单部分中的fields_for。让我们坚持使用railscast示例并说:

product.rb

  has_many:commments 
accepts_nested_attributes_for:comments

您可以在_fields上编辑评论。产品的html.erb:

 <%= f.fields_for:comments do | cf | %GT; 
<%=渲染'comments / fields',:f => cf%>
<%end%>

并确保您的评论视图中有部分字段。


How could you do what's covered in RyanB's Railscast on editing multiple records individually, using Formtastic? Formtastic doesn't use form_tag, which RyanB's method relies on.

解决方案

The semantic_form_for is just a wrapper around form_for so you can use the same parameters. Here is a formtastic version of 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 %>

Please note that you can use the form_for helpers as well in formtastic.

Update

If you like to use nested attributes as well it should work out of the box, using fields_for on the form partial. Lets stick with the railscast example and say that:

product.rb

has_many :commments
accepts_nested_attributes_for :comments

You can edit the comments on the _fields.html.erb of the products like:

<%= f.fields_for :comments do |cf| %>
  <%=render 'comments/fields', :f=>cf%>
<%end%>

And make sure you have a fields partial in your comments views.

这篇关于Railscast 198,但使用formtastic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:30