Rails 3 在单个表单中编辑多个记录
我已经被这个问题困扰了几天了。
我在 Railscasts Episode #198 上取得了一些成功,但那是针对 Rails 2 的。Rails 3 中进行了一些更改,因此在 Episode #198 中提供的代码将无法工作。
问题出在 edit_individual.html.erb 中:
原始代码(由 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 %>
修改后的代码(简单地将 fields_for 更改为 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 %>
在新代码中,每条记录都放置在自己的表单中,全部放在一个表单中单一形式(这是我只想要的)。
我的问题是,如何让 Railscasts Episode #198 提供的代码在 Rails 3 中工作?
这是我提到的 Railscast 的链接: http://railscasts.com/episodes/198-edit-multiple-individually
谢谢你, 艾伦·罗萨里奥
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了解决方案。只需要修改 Ryan @Railscasts: 提供的代码中的以下行
并将其更改为:
注意,
<%
已修改为<%=
。最终解决方案:
我想知道是否有人可以向我解释这个解决方案。据我了解,您只需要在
fields_for
前面添加<%
即可。艾伦·罗萨里奥
I found the solution. Just need to modify the following line in the code provided by Ryan @ Railscasts:
and change it to:
Notice, that the
<%
has been modified to<%=
.final solution:
I was wondering if anyone could explain this solution to me. From what I understand you should only need a
<%
in front of thefields_for
.c.allen.rosario
Rails 3 中从 <% fields_for 到 <%= fields_for 的变化是因为 form_for、form_tag 等使用 <% form... %> 令人困惑。 即使他们正在输出 html 代码。
在 Rails 3 中,由于它们输出 html 代码,因此使用 <%=。
请注意,您的第一行已被弃用:
应该相同。
对于所有表单标签都
The change in Rails 3 from <% fields_for to <%= fields_for is because it was confusing that form_for, form_tag, etc... were using <% form... %> even though they they were outputting html code.
With Rails 3, since they output html code, they use <%=.
Please note that your first line is deprecated:
should be
Same for all form tags.