Imagine a movie-rental website. Sensible REST urls might look like
this:
/movies/51
…and on the business side:
/rentals
/customers
To see the rentals for a particular customer or movie:
/customers/12/rentals
/movies/29/rentals
Unfortunately, adding the last two breaks routing with a cryptic error:
new_movie_url failed to generate from {:controller=>“movies”,
:action=>“new”}, expected: {:controller=>“movies”, :action=>“new”},
diff: {}
Here’s the routes.rb:
map.resources :movies do |movies|
movies.resources :rentals
end
map.resources :customers do |customers|
customers.resources :rentals
end
map.resources :rentals do |rentals|
rentals.resources :movies
end
The last resource map now makes new_movie_path() expect to receive a
Rental id. So, the cheap way to fix this is to always call
new_movie_path(1) instead, but it’s a bad hack. What’s the “right way”
to do these kinds of routes?
It also seems like we should have a better error message in place for
this kind of scenario. Throwing an exception that says I’ve passed all
of the right parameters is crazy.