ruby-on-rails - Railscast 198,但使用 formtastic

标签 ruby-on-rails ruby-on-rails-3 forms formtastic

你怎么能做 RyanB's Railscast 中涵盖的内容关于使用 Formtastic 单独编辑多个记录? Formtastic 不使用 form_tag,RyanB 的方法依赖于 form_tag。

最佳答案

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 helper 以及 formtastic .

更新

如果您也喜欢使用嵌套属性,它应该开箱即用,在表单部分上使用 fields_for。让我们坚持使用 railscast 示例并说:

产品.rb
has_many :commments
accepts_nested_attributes_for :comments

您可以编辑产品的 _fields.html.erb 上的评论,例如:
<%= f.fields_for :comments do |cf| %>
  <%=render 'comments/fields', :f=>cf%>
<%end%>

并确保您的评论 View 中有一个部分字段。

关于ruby-on-rails - Railscast 198,但使用 formtastic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6541224/

相关文章:

ruby-on-rails - 如何编写一个任务来在 Rails 3 中运行测试?

forms - Codeigniter - 设置复选框选中状态的值

ruby-on-rails - 如何在 Rails 项目中将 key 文件作为参数传递

ruby-on-rails - 为什么 ActionDispatch::Static 从生产中的中间件栈中移除?

ruby-on-rails - 如何测试一个对象是否是用 rspec 创建的?

ruby-on-rails-3 - 将 Base64 图像与 Carrierwave 结合使用

ruby - 如何测试 Twilio 应用程序?

javascript - 针对输入到表单输入字段中的特定文本并为其添加样式?

C# 如何从我的用户控件访问主窗体上的 setter 方法?

ruby-on-rails - 清理回形针错误消息