在多个地方使用 fields_for

发布于 2024-09-02 09:38:32 字数 1631 浏览 5 评论 0原文

我有一个简单的模型,

class Ad < ActiveRecord::Base
   has_many :ad_items
end

class AdItem < ActiveRecord::Base
   belongs_to :ad
end

我有一个“ads/new”视图,它显示了用于创建新广告并向其中添加一些项目的表单。

.html.erb 代码如下所示:

<% form_for @ad, do |ad_form| %>
   <!-- some html -->

   <% ad_form.fields_for :ad_items do |f| %>
      <%= f.text_area "comment", :class => "comment", :rows => "5" %>
   <% end %>

   <!-- some other html -->

   <% ad_form.fields_for :ad_items do |f| %>
      <% render :partial => "detailed_item_settings", :locals => {:f => f} %>
   <% end %>
<% end %>

当广告有一个项目时.. ...

def new
   @ad = session[:user].ads.build

   # Create one item for the ad. Another items will be created on the
   # client side
   @ad.ad_items.build

   # standard stuff ...
end

生成的 HTML 看起来像这样:

<form ... >
   <!-- some html -->

   <textarea id="ad_items_attributes_0_comment" name="ad[ad_items_attributes][0][comment]" />

   <!-- some other html -->

   <!-- "detailed_item_settings" partial's content -->
      <textarea id="ad_ad_items_attributes_1_desc" name="ad[ad_items_attributes][1][desc]" />
   <!-- end -->
</form>

正如代码中所述,由于 HTML 结构,我使用 fields_for 方法两次,我必须遵循

对于第二个“fields_for”调用,正如我所期望的,“item”的索引已经是 1,而不是 0。

就像,通过调用“fields_for”方法,一些内部计数器将会增加......

但这有点奇怪......

我试图设置 :index =>; fields_for 为 0,但一切保持不变...

这里出了什么问题?

I have a simple model

class Ad < ActiveRecord::Base
   has_many :ad_items
end

class AdItem < ActiveRecord::Base
   belongs_to :ad
end

I have an "ads/new" view, that shows me the form for creating the new ad and adding some items to it

The .html.erb code is like a following:

<% form_for @ad, do |ad_form| %>
   <!-- some html -->

   <% ad_form.fields_for :ad_items do |f| %>
      <%= f.text_area "comment", :class => "comment", :rows => "5" %>
   <% end %>

   <!-- some other html -->

   <% ad_form.fields_for :ad_items do |f| %>
      <% render :partial => "detailed_item_settings", :locals => {:f => f} %>
   <% end %>
<% end %>

When the ad has one item ...

def new
   @ad = session[:user].ads.build

   # Create one item for the ad. Another items will be created on the
   # client side
   @ad.ad_items.build

   # standard stuff ...
end

... resulting HTML, will look like this:

<form ... >
   <!-- some html -->

   <textarea id="ad_items_attributes_0_comment" name="ad[ad_items_attributes][0][comment]" />

   <!-- some other html -->

   <!-- "detailed_item_settings" partial's content -->
      <textarea id="ad_ad_items_attributes_1_desc" name="ad[ad_items_attributes][1][desc]" />
   <!-- end -->
</form>

As it states in the code, i use fields_for method twice, because of the HTML structure, that i must follow

For the second "fields_for" call, index for "item" is already 1, not 0, as i expect.

It's like, that by calling "fields_for" method, some internal counter will be incremented ...

But this is a little strange behaviour ...

I've tried to set :index => 0 for fields_for, but all stays the same ...

What's wrong here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

和我恋爱吧 2024-09-09 09:38:32

您可以为每个项目手动设置索引,但是您必须迭代项目才能获取项目索引:

  <% ad_form.fields_for :ad_items do |f| %>
     <%= f.text_area "comment", :class => "comment", :rows => "5" %>
  <% end %>
  ...
  <% ad_items.each_with_index do |item, i| %>
    <% ad_form.fields_for :ad_items, item, :child_index => i do |f| %>
      <% render :partial => "detailed_item_settings", :locals => {:f => f} %>
    <% end %>
  <% end %>

You can set index manually for each item, but you have to iterate through your items for that to get item index:

  <% ad_form.fields_for :ad_items do |f| %>
     <%= f.text_area "comment", :class => "comment", :rows => "5" %>
  <% end %>
  ...
  <% ad_items.each_with_index do |item, i| %>
    <% ad_form.fields_for :ad_items, item, :child_index => i do |f| %>
      <% render :partial => "detailed_item_settings", :locals => {:f => f} %>
    <% end %>
  <% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文