I doing some forms, and tried to made a nested form that way:
<%= form_for(@birth) do |f| %>
<%= f.text_area(:obs) %>
<%= f.fields_for :child_attributes do |ff| %>
<%= text_field(:child_attributes, :earring) %>
<% end %>
<% end %>
Then, in BirthsController, I can get the nested attributes that way:
child = params[:child_attributes]
But, if I change my nested form to:
<%= form_for(@birth) do |f| %>
<%= f.text_area(:obs) %>
<%= f.fields_for :child_attributes do |ff| %>
<%= ff.text_field(:earring) %>
<% end %>
<% end %>
It does not work.
What’s exactly the difference between them, and why the second way (that I think is more elegant) do not work?
Thanks
The
text_fieldmethod knows nothing about your object@birth, so the name of the field will just bechild_attributes, that’s why you can access it viaparams[:child_attributes], but that’s not what you want.You should use
ff.text_field. Then in yourBirthmodel, add: