views:

80

answers:

1

I have a problem in trying to link a stand with two checkboxes which are resident & communication, and they seem to be stored separately if more than one stand is choosen.

If I have one stand the parameters seem to be stored in the correct manner inside a hash of an array, but once I add another stand to that same array it seems to swap the hashes.my view is like this. And you have another option of adding another stand which calls the same partial which is:

- unless @stands.empty?
%tr#show_stands.contentText
%td{:width => "27%", :valign => "top"}
  Select Stand For Ownership
%td{:width => "15%", :valign => "top"}
  = collection_select "owned_stands[]", "stand_id", @stands, :id, :erf_no_rr_no,  options ={:prompt =>"Please select stand..."}, {:class => 'dropdownSelect'}
   
   
%td{:valign => "top"}
  = check_box_tag "owned_stands[][owner_comm_list]"
  Add to Owners Communication list
  = check_box_tag "owned_stands[][resident_comm_list]"
  Add to Residents Communication list

I am expecting an array that contains a two hashes or more depending on the number of stands that I have selected. If I have selected one stand and both checkboxes I expect the hash lie:

([{"stand_id" => "1", "resident_comm_list" => "1", "owner_comm_list" => "1"}])

And if I have two I expect:

([{"stand_id" => "1", "resident_comm_list" => "1", "owner_comm_list" =>    "1"},{"stand_id" => "2", "resident_comm_list" => "1", "owner_comm_list" => "1"}])

But now if I select two stands I find:

 ([{"stand_id" => "1", "resident_comm_list" => "1"}, {"owner_comm_list" =>    "1"},{"stand_id" => "2"},{"resident_comm_list" => "1", "owner_comm_list" => "1"}])

And it is giving me errors inside my controller when I have to loop through the hashes to select the stand_id.

+1  A: 

The problem lies in that your check_box_tags do not properly define where in the params hash they should fit in.

Rails usually makes it easy to get all the nesting straight, without needing you to resort to hard coding field ids. Unfortunately these convenience methods fail, when you're trying to use an array of checkboxes. Because of the checkbox gotcha concerning default values and arrays.

Your use of the check_box_tag avoids the gotcha, but requires you to fill in the information usually supplied by the form object.

It looks to me that all of your fields from multiple partials are being added to the same params array. The way Rails handles duplicate params in an array is to start another index.

Have a good look at the source produced, to use as clues in getting the correct result.

The code you posted required too much effort to get to a point where I could play around with it. So I can't guarantee this will work. Also without posting controller code, there's no way to tell why it's throwing errors.

Regardless the solution you want to add an index to the params for each partial.

You want to do something like this. Where index is a unique value to each partial.

- unless @stands.empty?
%tr#show_stands.contentText
%td{:width => "27%", :valign => "top"}
  Select Stand For Ownership
%td{:width => "15%", :valign => "top"}
  = collection_select "owned_stands[#{index}][]", "stand_id", @stands, :id, :erf_no_rr_no,  options ={:prompt =>"Please select stand..."}, {:class => 'dropdownSelect'}
   
   
%td{:valign => "top"}  
  = check_box_tag "owned_stands[#{index}][owner_comm_list]"
  Add to Owners Communication list
  = check_box_tag "owned_stands[#{index}][resident_comm_list]"
  Add to Residents Communication list

It will produce params hash like these: For one stand with both checkboxes:

params["owned_stands"] =
  {"0" =>  
    {"stand_id" => 1, "owner_comm_list" => 1, "resident_comm_list" => 1}
  }

For two stands with both checkboxes:

prams[owned_stands] = 
 {
  "0" =>  
    {"stand_id" => 1, "owner_comm_list" => 1, "resident_comm_list" => 1}, 
  "1" => 
    {"stand_id" => 2, "owner_comm_list" => 1, "resident_comm_list" => 1}
 }

You might want to look into accepts_nested_attributes_for and nested fields_for usage. They simplify this kind of thing in both the view and controller, but still fall prey to the check box gotcha.

EmFi