Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: submit form with nested attributes throws 'couldn't find entity with id=xxx'
    primarykey
    data
    text
    <p>I got the following model classes:</p> <pre><code>class Movie &lt; ActiveRecord::Base has_and_belongs_to_many :actors accepts_nested_attributes_for :actors, :allow_destroy =&gt; true ... end </code></pre> <p>and</p> <pre><code>class Actor &lt; ActiveRecord::Base has_and_belongs_to_many :movies ... end </code></pre> <p>movies/_form.html.haml view contains the nested form for actors:</p> <pre><code>... = form_for @movie do |movie_form| = movie_form.fields_for :actors do |actor_form| = actor_form.text_field :id, "Id" = link_to_remove_fields "remove", actor_form = link_to_add_fields movie_form :actors ... = movie_form.submit 'Save' </code></pre> <p><code>link_to_remove_fields</code> and <code>link_to_add_fields</code> are helper methods performing javascript calls, adding new fields for new actors or removing actors.</p> <p>The controller:</p> <pre><code> class MovieController &lt; ApplicationController #POST form def create @movie = Movie.new(params[:movie]) ... end #GET nearly empty form with one field for actor def new @movie = Movie.new 1.times {@movie.actors.build} respond_to do |format| format.html format.json { render json: @movie } end end end </code></pre> <p>The problem: When 'Save' is pressed (with only one actor with id=718, for example) and the form is submitted, the <code>@movie = Movie.new(params[:movie])</code> line of the controller throws the following error:</p> <blockquote> <p>Couldn't find Actor with ID=718 for Movie with ID=</p> </blockquote> <p>I'm sure that there is entity with id=718 in the Actor database.</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.
 

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