Hey All
I’m new to the whole RESTful way of thinking so perhaps I’m missing the
bigger picture here but…
I’ve got a habtm relationship that I’d really like to control RESTfully
but I’m not sure how best to handle it.
films <=> genres
I can’t come up with any logical model for a hmt relationship. How
would you handle that? Create a films_genres model perhaps? Is there a
better way?
Thanks!
Tim
REST doesn’t really apply to the model level, as far as I know (I’m a
noob myself with the concept). It’s at the controller level.
Set up your habtm models normally, using the Agile Rails book as an
example if necessary.
Then implement your RESTful routes which will give you URLs like this:
/films/562/genres/
/genres/17/films/
The routes would look like this:
map.resources :films do |film|
film.resources :genres, :prefix_path => ‘film_’
end
map.resources :genres do |genre|
genre.resources :films, :prefix_path => ‘genre_’
end
This will allow you to use helpers like film_genres_path(@film) to link
to a list of genres for a specific film. Or, “form_for :film, :url =>
genre_films_path(@genre)” to add a new film to a genre. This last
command may need (@genre,nil) as arguments I’ve found… the RESTful
stuff seems to sometimes need a blank argument explicitly when there are
similar routes configured.
I’ve got a habtm relationship that I’d really like to control
RESTfully
but I’m not sure how best to handle it.
REST hasn’t changed the way habtm relationships work. You can implement
the habtm models as before. Then create the routes like so:
map.resources :genres do |genre|
genre.resources :films
end
If you scaffold the resources, they’ll need a little tweaking to get the
paths right but there’s plenty of information on how to proceed out
there. (There’s a fine example in AWDWR 2nd.)
Roderick
–
Nedforce Informatica Specialisten B.V.
+31 (0)53 4500225