views:

1378

answers:

1

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?

+1  A: 

You must give each order item a unique index when you call fields_for. If you're calling fields_for in this manner, you need to keep track of the index of the array you pass to fields_for. Rails can do this for you by using nested forms.

The solution is to use nested forms.

<%form_for :order do |f|%>
  Form stuff for this particular order.
  If @order.order_items is empty you may need to build one before the next line.
  <%f.fields_for :order_items do |oi_f| %>
    Form stuff for this particular order_item (prefixed with oi_f.)
    <%Colour.all.each do |colour| %>
      <%=oi_f.radio_tag(:colour_id, colour.id)%>
    <%end%>
  <%end%>
<%end%>

Looks like you're posting to the orders_controller so this should be a drop in replacement.

EmFi
Thanks, this was exactly it! I had tried manually adding index values using Firebug but was getting the error I mentioned, turned out this was because I had other form fields and was mixing index and non-indexed name attributes.
roryf