Rails 3以单一形式编辑多个记录 [英] Rails 3 Edit Multiple Records in a Single Form
问题描述
我已经在Railscasts Episode#198上取得了一些成功,但那是一个用于Rails 2.在Rails 3中有一些变化,使得它在第#198集中提供的代码将无法工作。
问题在于edit_individual.html .erb:
原始代码(由Ryan @ Railscasts提供):
<%form_tag update_individual_products_path,:method => :put do%>
<@products%中产品的百分比>>
<%fields_forproducts [],product do | f | %GT;
< h2><%= h product.name%>< / h2>
<%=呈现字段,::f => f%>
<%end%>
<%end%>
< p><%= submit_tag提交%>< / p>
<%end%>
修改后的代码(简单地将fields_for更改为form_for):
<%form_tag update_individual_products_path,:method => :put do%>
<@products%中产品的百分比>>
< h2><%= h product.name%>< / h2>
<%=呈现字段,::f => f%>
<%end%>
<%end%>
< p><%= submit_tag提交%>< / p>
<%end%>
在新的代码中,每条记录放置在它们自己的一个表单内, (这是我唯一想要的)。
我的问题是,我怎样才能让Railscasts Episode#198提供的代码在Rails 3中工作?
这里是我提到的Railscast的链接:
http://railscasts.com/episodes/198-edit-multiple- inddividual
谢谢你,
c。 allen.rosario
我找到了解决方案。只需要修改Ryan @ Railscasts提供的代码中的以下行:
<%fields_forproducts [],产品do | f | %GT;
并将其更改为:
<%= fields_forproducts [],product do | f | %GT;
请注意,<%
有已被修改为<%=
。
最终解决方案:
<%form_tag update_individual_products_path:method => :put do%>
<@products%中产品的百分比>>
<%= fields_forproducts [],product do | f | %GT;
< h2><%= h product.name%>< / h2>
<%end%>
<%end%>
< p><%= submit_tag提交%>< / p>
<%end%>
我想知道是否有人可以向我解释这个解决方案。据我所知,你应该只需要在 fields_for
前加上一个<%
。
c.allen.rosario
I've been stuck on this problem for a couple of days now.
I've have some success with Railscasts Episode #198, but that one is for Rails 2. There have been some changes in Rails 3 that make it so the code provided in Episode #198 won't work.
The problem lies within the edit_individual.html.erb:
Original Code (provided by Ryan @ Railscasts):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in @products %>
<% fields_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
Modified Code (simply changed fields_for to form_for):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in @products %>
<% form_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
In the new code, each record is placed within a form of their own, all inside one single form (which is the one I only want).
My question is, how can I get the code provided by Railscasts Episode #198 to work in Rails 3?
Here is a link to the Railscast I mentioned: http://railscasts.com/episodes/198-edit-multiple-individually
Thank You, c.allen.rosario
I found the solution. Just need to modify the following line in the code provided by Ryan @ Railscasts:
<% fields_for "products[]", product do |f| %>
and change it to:
<%= fields_for "products[]", product do |f| %>
Notice, that the <%
has been modified to <%=
.
final solution:
<% form_tag update_individual_products_path :method => :put do %>
<% for product in @products %>
<%= fields_for "products[]", product do |f| %>
<h2><%= h product.name %></h2>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
I was wondering if anyone could explain this solution to me. From what I understand you should only need a <%
in front of the fields_for
.
c.allen.rosario
这篇关于Rails 3以单一形式编辑多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!