I asked this question earlier but I'm going to rephrase it. I have a worksheet that a user can create many targets, and each target has many attributes (selects that specify attribute_id's). Each target is added through jscript. The multi-model screencast from ryan bates explains how to add multiple children on the fly, but doesn't delve into multiple parents. The code
<% fields_for "target_attributes[]", Target.new do |target| -%>
<%= target.select("id",@classification_hash[classification_type.id].collect{|c| [c.name,c.id]}
Gives me the select element ( form_for :worksheet)
<select id="worksheet__target_id" class="selectbox" name="worksheet[target_attributes][][attribute_id]">
and puts all my target attributes into an array of hashes in the passed params, and that's fine. So I thought I could take this logic and have a nested fields_for, to then put all of my targets & their attributes into their own array
<% fields_for "worksheet[]",Worksheet.new do |worksheet| -%>
<% worksheet.fields_for "target_attributes[]", Target.new do |target| -%>
<%= target.select("id",@classification_hash[classification_type.id].collect{|c| [c.name,c.id]}
Gives me the form
<select id="worksheet__target_id" class="selectbox" name="worksheet[][target_attributes][][attribute_id]">
So, I figured then all of my targets would be in their own array and All would be gravy, but I get this in my params:
worksheet"=> [{"target_attributes"=>[{"id"=>"12"}]}, {"target_attributes"=>[{"id"=>"15"}]}, {"target_attributes"=>[{"id"=>"17"}]}, {"target_attributes"=>[{"id"=>"14"}]}, {"target_attributes"=>[{"id"=>"16"}]}, {"target_attributes"=>[{"id"=>"17"}]}]
(Note, there's 3 target_attribute selects for each target, so that's listing two targets with 3 attributes) So, obviously I can't separate each target this way in the controller to create new target objects. Can anyone help me out? I'd love a form that looks like this:
<select id="worksheet__target_id" class="selectbox" name="worksheet[target_attributes][0][][attribute_id]">
( 3 times, for each target attribute)
<select id="worksheet__target_id" class="selectbox" name="worksheet[target_attributes][1][][attribute_id]">
etc etc for each target added
Then I have a new "row" essentially for each new task. but I can't see any way of doing this without some messy javascript that evaluates the select names and modifies it based on some javscript iterator variable. Any thoughts are greatly appreciated