Note that there are some explanatory texts on larger screens.

plurals
  1. PORails forms, create multiple parents and children in one form
    primarykey
    data
    text
    <p>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</p> <blockquote> <p><code>&lt;% fields_for "target_attributes[]", Target.new do |target| -%&gt;</code> <code>&lt;%= target.select("id",@classification_hash[classification_type.id].collect{|c| [c.name,c.id]}</code></p> </blockquote> <p>Gives me the select element ( form_for :worksheet)</p> <blockquote> <p><code>&lt;select id="worksheet__target_id" class="selectbox" name="worksheet[target_attributes][][attribute_id]"&gt;</code></p> </blockquote> <p>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 &amp; their attributes into their own array</p> <blockquote> <p><code>&lt;% fields_for "worksheet[]",Worksheet.new do |worksheet| -%&gt;</code> <code>&lt;% worksheet.fields_for "target_attributes[]", Target.new do |target| -%&gt;</code> <code>&lt;%= target.select("id",@classification_hash[classification_type.id].collect{|c| [c.name,c.id]}</code></p> </blockquote> <p>Gives me the form</p> <blockquote> <p><code>&lt;select id="worksheet__target_id" class="selectbox" name="worksheet[][target_attributes][][attribute_id]"&gt;</code></p> </blockquote> <p>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:</p> <pre>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"}]}] </pre> <p>(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:</p> <p><code>&lt;select id="worksheet__target_id" class="selectbox" name="worksheet[target_attributes][0][][attribute_id]"&gt;</code> ( 3 times, for each target attribute)</p> <p><code>&lt;select id="worksheet__target_id" class="selectbox" name="worksheet[target_attributes][1][][attribute_id]"&gt;</code> etc etc for each target added</p> <p>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 Javascript iterator variable. Any thoughts are greatly appreciated</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload