Hi Leuddens
This is a tricky and lengthy one that’s about to blow my current project
to little glittering pieces
I’m using select multiple in a subform (as described in Ryan’s excellent
Complex Forms screencasts on railscasts.com. This is the relation:
Offer has_and_belongs_to_many :parts
Part has_many :manufacturers
The Offer form allows you to add/remove parts. And the _part.html.erb
partial contains one text_input “name” and one select-multiple
“manufacturer_ids”.
The object (form element) names in the _part partial are correctly named
“offer[part_attributes][]” and the select multiple adds
“[manufacturer_ids][]” to it, which results in the nice to look at…
offer[part_attributes][][manufacturer_ids][]
I create two parts and select two manufacturers for each part, hit
submit and take a look at the params hash:
[
{“manufacturer_ids”=>[], “name”=>“FIRST PART”},
{“manufacturer_ids”=>[]},
{“manufacturer_ids”=>[], “name”=>“SECOND PART”},
{“manufacturer_ids”=>[]},
{“manufacturer_ids”=>[]},
{“manufacturer_ids”=>[]}
]
That doesn’t look too good. Apparently, the form parameters are not
parsed correctly into the params hash at all.
After some research, I found the follwing patch:
http://dev.rubyonrails.org/ticket/10101
Once applied, the output looks slightly better:
params[:offer][:part_attributes] =>
[
{“manufacturer_ids”=>[“209”], “name”=>“FIRST PART”},
{“manufacturer_ids”=>[“254”], “name”=>“SECOND PART”},
{“manufacturer_ids”=>[“238”]},
{“manufacturer_ids”=>[“209”]},
{“manufacturer_ids”=>[“254”]},
{“manufacturer_ids”=>[“238”]}
]
But this is still not what ActiveRecord needs in order to write the
records correctly. Even worse: The nesting (which manufacturer_ids
belong to which part) are lost, so I can’t even tweak the params hash
manually in the controller.
I can’t believe that I’m the only one facing this issue. As a matter of
fact, if you decide to have a another complex form within yet a complex
form partial, you will face the same challenge.
Unfortunately, the UrlEncodedPairParser - that seems responsible for
this - is too magic for my skills.
Please help, somebody, I’m really stuck knee deep with this one.
Many thanks! -sven
PS:
Could Ruby 1.8’s Array handling (random member order) have anything to
do with this? If so, Ruby 1.9 might help as it maintains Array member
order - unfortunately, I have no install of Ruby 1.9 at hand to verify
this.