Newbie question, you’ve been warned!
I’m trying to implement a sample Rails app with a many-to-many association, people owning movies, and I’m trying to figure out how exactly to implement the UI for it. It’s my understanding that REST requires everything to be a resource, so in this case “User” (person), “Movie” and “Possession” (the joint table) (oh, the puns).
Now the interesting part, the UX. Let’s say I have a user dashboard where all of your movies are listed.
Let’s say the user wants to add a movie that he owns. How do you do this in REST? It’s trivial with a custom action that one could add to the User controller, but the point is not to go beyond the basic 7 REST actions, right? Therefore I’d have to first do a “new” on a movie and then do a “new” on a possession, which are two operations. How do I collapse them into one?
Basically I feel I’m not quite understanding how to maintain REST as soon as multiple models are involved and would appreciate a tip.
Thanks!
Happily, Rails has some magic just for this common scenario. Assuming a model like this:
Your view:
Basically this form will
POSTtoMoviesController#createand will pass alongcurrent_user.idas auser_idparameter that (the default)MoviesController#createwill know to associate with theMovieit creates. Take a look at the documentation forFormBuilder#form_forfor more information.You could also do this the other way around, by the way:
And the view:
In this case the form will submit to
UsersController#updateand its parameters will look like this:…and the controller will know to create the
Movieobject. For more information check the documentation forFormHelper#fields_for.