According to the Rails Guides and this Railscasts episode, when there s a one-to-many association between two objects (e.g. Project
and Task
), we can submit multiple instances of Task
together with the Project
during form submission similar to this:
<% form_for :project, :url => projects_path do |f| %>
<p>
Name: <%= f.text_field :name %>
</p>
<% for task in @project.tasks %>
<% fields_for "project[task_attributes][]", task do |task_form| %>
<p>
Task Name: <%= task_form.text_field :name %>
Task Duration: <%= task_form.text_field :duration %>
</p>
<% end %>
<% end %>
<p><%= submit_tag "Create Project" %></p>
<% end %>
This will result multiple copies of an HTML block like this in the form, one for each task:
<p>
Task Name: <input name="project[task_attributes][name]">
Task Duration: <input name="project[task_attributes][duration]">
</p>
My question is, how does Rails understand which
(project[task_attributes][name], project[task_attributes][duration])
belong together, and packing them into a hash element of the resulting array in params
? Is it guaranteed that the browsers must send the form parameters in the same order in which they appear in the source?