Let's say I have a cat model and a life model. And let's say a cat (@cat
) has_many
lives, and a cat accepts_nested_attributes for
a life.
Now, if I wanted to update 7 lives (@lives
) at once, using one form_for(@cat)
, how would that form look like? This is what I've tried, but in this form only the attributes for the last life are passed to the params hash:
<%= form_for(@cat) do |f| %>
<% @lives.each do |life| %>
<%= f.fields_for(life) do |l| %>
<%= l.input :date_of_birth, as: :date %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
You need to build the attributes in your controller
@cat = Cat.find(<criteria>)
@cat.lives.build
In your example, you have a loop inside a loop. Try this:
<%= form_for(@cat) do |f| %>
<%= f.fields_for(:lives) do |l| %>
<%= l.input :date_of_birth, as: :date %>
<% end %>
<%= f.submit %>
<% end %>