Rails 3 在单个表单中编辑多个记录

发布于 2024-10-06 17:04:14 字数 1385 浏览 5 评论 0原文

我已经被这个问题困扰了几天了。

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-10-13 17:04:14

我找到了解决方案。只需要修改 Ryan @Railscasts: 提供的代码中的以下行

<% fields_for "products[]", product do |f| %>

并将其更改为:

<%= fields_for "products[]", product do |f| %>

注意,<% 已修改为 <%=

最终解决方案:

<% 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 %>  

我想知道是否有人可以向我解释这个解决方案。据我了解,您只需要在 fields_for 前面添加 <% 即可。

艾伦·罗萨里奥

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

兲鉂ぱ嘚淚 2024-10-13 17:04:14

Rails 3 中从 <% fields_for 到 <%= fields_for 的变化是因为 form_for、form_tag 等使用 <% form... %> 令人困惑。 即使他们正在输出 html 代码。
在 Rails 3 中,由于它们输出 html 代码,因此使用 <%=。

请注意,您的第一行已被弃用:

<% form_tag update_individual_products_path, :method => :put do %>

应该相同。

<%= form_tag update_individual_products_path, :method => :put do %>

对于所有表单标签都

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:

<% form_tag update_individual_products_path, :method => :put do %>

should be

<%= form_tag update_individual_products_path, :method => :put do %>

Same for all form tags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文