Hi - I’m trying to figure out how to group nested fields in the form
such that they all submit with a certain order. I followed the complex
forms railscast (#75 Complex Forms Part 3 - RailsCasts), but I’m trying to
submit a nested model with more than a single field.
<% fields_for ‘project[task_attributes][]’, task do |tf| %>
Name: <%= tf.text_field :name, :index => nil %>
Start: <%= tf.text_field :start, :index => nil %>
End: <%= tf.text_field :end, :index => nil %>
<% unless task.new_record? %>
<% tf.hidden_field :id, :index => nil %>
<% end %>
<% end -%>
When I submit, params looks strange and is completely disorganized:
{ “project” =>
{ “name” => “test”,
“task_attributes” => [{ “start” => “10:00” },
{ “start” => “12:00” },
{ “end” => “10:30”, “start” => “11:00” },
{ “end” => “11:30” },
{ “end” => “12:30”, “id” => “1” },
{ “id” => “2”, “name” => “mow lawn” },
{ “name” => “buy milk” },
{ “name” => “clean room” }] }}
If I ommit the :index => :nil, I get an error “conflicting types for
parameter containers. Expected an instance of Array but found an
instance of Hash” called on task_attribues. What I need is exactly that
- a hash grouping each task by id with it’s attributes:
{ “task_attributes” =>
{ “1” => { “name” => ‘buy milk’,
“start” => ‘10:00’,
“end” => ‘10:30’ }
“2” => {…}…}
I’m feeling like I’m missing something big here… Any help will be
great - thanks.