Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating nested forms with existing data in inner form using rails 3
    text
    copied!<p>I'm trying to get a nested form view to update properly. This is however causing problems when the second form has existing data.</p> <p>I'm using accepts_nested_attributes_for and nested_form_for. The second which only purpose is to dynamically add the form element using js. See <a href="https://github.com/madebydna/nested_form" rel="nofollow">github for more</a></p> <p>The error I'm getting is:</p> <pre><code>Couldn't find Muscle with ID=3685340 for Exercise with ID=212831413 </code></pre> <p>I've tried to manually do the updating but my code didnt really work and I'm under the impression that it shouldnt be needed since rails is suppose to take care of it under the hood.</p> <p>The idea is that: Exercises has many Muscles through Targets And from within the Exercise form I want to be able to add target muscles.</p> <p>My models:</p> <pre><code>class Exercise &lt; ActiveRecord::Base has_many :targets, :dependent =&gt; :destroy has_many :muscles, :through =&gt; :targets accepts_nested_attributes_for :muscles, :reject_if =&gt; :all_blank ... end class Target &lt; ActiveRecord::Base belongs_to :exercise accepts_nested_attributes_for :exercise, :update_only =&gt; true belongs_to :muscle end class Muscle &lt; ActiveRecord::Base has_many :targets, :dependent =&gt; :destroy has_many :exercises, :through =&gt; :targets end </code></pre> <p>My (haml) view:</p> <pre><code>%p %b Target(s): = f.fields_for :muscles do |e| = e.collection_select :id, Muscle.all, :id, :name = e.link_to_remove "-remove" = f.link_to_add "Add target muscle", :muscles </code></pre> <p>And finally my failing controller:</p> <pre><code> def update @exercise = Exercise.find(params[:id]) @exercise.user = current_user params[:exercise][:muscles_attributes].each { |id, muscle| target = Target.where(:exercise_id =&gt; @exercise.id , :muscle_id =&gt; muscle[:id]).first if target &amp;&amp; !(muscle[:_destroy] == "false") puts "!!&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;destroy target #{target.exercise_id} #{target.muscle_id}" target.destroy else if !target t = @exercise.targets.build(:muscle_id =&gt; muscle[:id]) t.save end end } respond_to do |format| if @exercise.update_attributes(params[:exercise]) format.html { redirect_to(@exercise, :notice =&gt; 'Exercise was successfully updated.') } format.xml { head :ok } else format.html { render :action =&gt; "edit" } format.xml { render :xml =&gt; @exercise.errors, :status =&gt; :unprocessable_entity } end end end </code></pre> <p>Please let me know if I should expose more of my code (the final result will be opensource anyway) ill happily push to github or whatever is requested, thanks in advance.</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