Note that there are some explanatory texts on larger screens.

plurals
  1. PONested attributes has_many passing and collecting ids to build model
    text
    copied!<p>I have this form: </p> <pre><code>&lt;% form_tag add_expt_details_samples_path, :method =&gt; :post do %&gt; &lt;% for sample in @samples %&gt; &lt;% if sample.study_id == @study.id %&gt; &lt;% fields_for "samples[]", sample do |form| %&gt; &lt;fieldset&gt; &lt;legend&gt;Sample Name: &lt;%= sample.name %&gt;&lt;/legend&gt; &lt;p&gt;&lt;center&gt;&lt;%= form.label :sample_title %&gt; &lt;%= form.text_field :sample_title, :size =&gt; 25 %&gt;&lt;/center&gt;&lt;/p&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;%= form.label :taxon_id %&gt; &lt;%= form.text_field :taxon_id, :size =&gt; 15 %&gt; &lt;/td&gt; &lt;td&gt; &lt;%= form.label :scientific_name %&gt; &lt;%= form.text_field :scientific_name, :size =&gt; 20 %&gt; &lt;/td&gt; &lt;td&gt; &lt;%= form.label :common_name %&gt; &lt;%= form.text_field :common_name, :size =&gt; 15 %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;p&gt; &lt;center&gt; &lt;%= form.label :sample_descripition %&gt;&lt;br \&gt; &lt;%= form.text_area :description %&gt; &lt;/center&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;p&gt;&lt;center&gt;&lt;%= submit_tag "Next" %&gt;&lt;/center&gt;&lt;/p&gt; &lt;% end %&gt; </code></pre> <p>I would like to collect the samples[] in add_expt_details action so that I could render a post method view for adding new model records for the samples[] ids.</p> <p>my action is as follows</p> <pre><code>def add_expt_details # Collect the samples that were sent @expt_samples = Sample.find(params[:samples]) # @expt_samples.each do |samp| # samp.expts.build # end end </code></pre> <p>and then send them to a create action where the sample details are updated and the expt values can be created. </p> <pre><code>def create_study @samples = Sample.update(params[:samples].keys, params[:samples].values).reject { |p| p.errors.empty? } if @samples.empty? flash[:notice] = "Samples updated" redirect_to :action =&gt; "add_expt_details", :anchor =&gt; "ok" else render :action =&gt; 'edit_individual' end end </code></pre> <p>is there a way to achieve this, I have used accepted_nested_attributes_for :expts, :dependent => :destroy. But I seem to be wrong some where and I do not want to have a form with many fields. </p> <p>The error message I get is "Unknown key(s): 3, 6, 12"</p> <p>All suggestions are appreciated. </p>
 

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