Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the index of children in a nested form

In a Rails 3.2 app I'm using Simple Form to create a complex form.

The form/ model accepts_nested_attributes_for, and I need to get the index of child objects.

The models:

class Project
  has_many :tasks
  accepts_nested_attributes_for :tasks
end

class Task
  belongs_to :project
end

The form

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

How do I correctly get the index?

like image 474
Andy Harvey Avatar asked Dec 26 '22 21:12

Andy Harvey


1 Answers

You can use this:

<%= simple_form_for @project do |f| %>
    <%= f.simple_fields_for :tasks do |builder| %>
        <%= builder.index %>
    <% end %>
<% end %>
like image 84
Abhishek Avatar answered Jan 17 '23 09:01

Abhishek