I'm having a problem with nested model forms that contain radio buttons, when I have multiple models all the radio buttons are treated as being in the same group.
My model contains a has_many relationship like this:
class Order < ActiveRecord::Base
has_many :order_items
accepts_nested_attributes_for :order_items
end
Class OrderItem < ActiveRecord::Base
belongs_to :order
end
I then have a partial that creates the OrderItem
model form using
<% fields_for "order[order_items_attributes][]", order_item do |f| %>
And contained within this form is a group of radio buttons created inside a for loop with
radio_button_tag "order[order_items_attributes][][colour_id]", "#{colour.id}"
This works fine when there is only one child, however as soon as I insert multiple children all the radio buttons belong to the same group as they all have the same attribute name="order[order_items_attributes][][colour_id]"
. This is all on a new model form so I can't use array indexes (name="order[order_items_attributes][0][colour_id]"
) as Rails gives the error expected Hash (got Array) for param 'order_items_attributes'
I was wrong about that last part, error was because I was mixing indexed and non-indexed name attributes. Adding index values was the key to solving this.
Here is the contents of the params[:order]
hash when only one nested model is present:
{"order_items_attributes"=>
[{"size"=>"Small",
"colour_id"=>"4"],
"first_name"=>"sdf",
"last_name"=>"sdf",
"email"=>"[email protected]"}
And when two nested models are present:
{"order_items_attributes"=>
[{"size"=>"Small",
"colour_id"=>"4"},
{"size"=>"Small"}],
"first_name"=>"sdf",
"last_name"=>"sdf",
"email"=>"[email protected]"}
As you can see only the first order_item
has it's colour_id attribute. This occurs regardless of which model the selected radio button belonged to (which makes sense).
How can I render the radio buttons such that it creates a separate group for each child model?