haml - Rails: submit form with nested attributes throws 'couldn't find entity with id=xxx' -
i got following model classes:
class movie < activerecord::base has_and_belongs_to_many :actors accepts_nested_attributes_for :actors, :allow_destroy => true ... end and
class actor < activerecord::base has_and_belongs_to_many :movies ... end movies/_form.html.haml view contains nested form actors:
... = form_for @movie |movie_form| = movie_form.fields_for :actors |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' link_to_remove_fields , link_to_add_fields helper methods performing javascript calls, adding new fields new actors or removing actors.
the controller:
class moviecontroller < applicationcontroller #post form def create @movie = movie.new(params[:movie]) ... end #get empty form 1 field actor def new @movie = movie.new 1.times {@movie.actors.build} respond_to |format| format.html format.json { render json: @movie } end end end the problem: when 'save' pressed (with 1 actor id=718, example) , form submitted, @movie = movie.new(params[:movie]) line of controller throws following error:
couldn't find actor id=718 movie id=
i'm sure there entity id=718 in actor database.
the easiest thing if trying associating existing actors new movie not use accepts_nested_attributes @ all.
one of things actors associations actor_ids, return ids of associated actors, or allows set list of associated actors.
to submit array via rails form, name of input must end [], example if have input name movie[actor_ids][] , value 1, you'll get
{'movie' => {'actor_ids' => ['1']}} in controller. if want submit second actor, create input same name movie[actor_ids][]. use select box multiple attribute set, can bit unfriendly users when there more few options pick from.
Comments
Post a Comment