4

Rails 3.2 アプリでは、Simple Formを使用して複雑なフォームを作成しています。

フォーム/モデルaccepts_nested_attributes_for、および子オブジェクトのインデックスを取得する必要があります。

モデル:

class Project
  has_many :tasks
  accepts_nested_attributes_for :tasks
end

class Task
  belongs_to :project
end

フォーム

<%= simple_form_for @project do |f| %>
  <%= f.simple_fields_for :tasks do |builder| %>
    ## I need to get the index of each object built via builder
  <% end %>
<% end %>

インデックスを正しく取得するにはどうすればよいですか?

4

2 に答える 2

11

これを使用できます:

<%= simple_form_for @project do |f| %>
    <%= f.simple_fields_for :tasks do |builder| %>
        <%= builder.index %>
    <% end %>
<% end %>
于 2015-07-30T11:18:26.590 に答える
-1

これは fields_for 経由で直接行うことはできないようです。代わりに、次のアプローチが機能します。

<%= simple_form_for @project do |f| %>
  <% @project.tasks.each.with_index do |task, index| %>
    <%= f.simple_fields_for :tasks, task do |builder| %>

      <%= index %>  #get the index here!!

    <% end %>
  <% end %>
<% end %>
于 2013-09-06T08:34:11.577 に答える