-1

I'm taking my first steps "on rails". I looked up several questions on stackoverflow and used google A LOT, but still I can't seem to solve my problem.

I´m trying to write a rails app that lets you manage a list of movies, and a list of actors. Movies can have multiple actors (and hence an actor can star in multiple movies).I have two models Movie and Actor as well as the joint ActorsMovie model. As you can see, I want to establish a Two Many-To-Many relationship.

Please find the relevant files here: https://gist.github.com/ecksma/f09d2a6ec631e46eafe9

Even though I added params.require(:movie).permit(:title, :year, actors_attributes:[:actor_id,:name] ) to movies_controller, I get an Unpermitted parameter: actors error when I try to create a new movie with an actor.

Command line output https://gist.github.com/ecksma/f09d2a6ec631e46eafe9#file-command_line_output

I have the same problem when I try to create a new actor with movies. The actor is created but I get Unpermitted parameter: movies, although I added params.require(:actor).permit(:name, movies_attributes:[:title,:year,:movie_id]) to actors_controller.

Forms and basic CRUD seems to work, except that I can't seem to hook up movies with actors and vice versa.

Any suggestions on what I´m doing wrong?

  • can you post the entire controller and the form please? it seems you have a wrong field there – matanco Oct 16 '15 at 18:51
  • If you follow this link [Form,Controller,Models etc](https://gist.github.com/ecksma/f09d2a6ec631e46eafe9), you should find all relevant files in a gist (such as the entire controller). Thank you! – user3497221 Oct 16 '15 at 19:05

1 Answers1

0

Did you try to add accepts_nested_attributes_for :actors in Movie model?

mandar.gokhale
  • 1,876
  • 1
  • 18
  • 37
  • I just tried and it works. Thank you so much! That was really helpful. Nonetheless there is a strange behaviour when editing movies. Everytime I add an actor to an existing movie, the actor that was initially added with the movie gets listed twice afterwards. – user3497221 Oct 16 '15 at 19:33
  • You need to permit "id" in "actors_attributes". When you do MyModel.update_attributes(params) it checks for "id". If "id" is present record gets updated else new record is created. – mandar.gokhale Oct 16 '15 at 19:38
  • You are great, that really helped me out! Thanks again. – user3497221 Oct 16 '15 at 19:43
  • Unfortunatly I can't. It says once I earned 15 reputation my votes will be displayed publicly. I don't have that, yet. :/ – user3497221 Oct 16 '15 at 19:50