Hi all,
I’ve been scouring the web trying to find an answer to this problem, but
most of the discussions I’ve found seem old, inconclusive or both.
I’m trying to create a resource(REST) based app that has users and
adverts posted to categories. I’d like to have my routes set up like
this:
…
map.resources :users do |users|
users.resources :adverts, :name_prefix => “users_”
end
map.resources :categories do | categories |
categories.resources :adverts, :name_prefix => “categories_”
end
…
Which would allow me to use URL like:
/users/1/adverts/new
/users/1/adverts/15;edit
/categories/35/adverts/84;edit
Which is all fine. However, in my adverts views, I need to generate the
paths to the actions in the correct parent resource. Obviously, I can’t
do something like user_advert_path(@user, @advert), as it will be
incorrect when I render the view from a category parent.
I know I could do a workaround by having a base route to /adverts
(map.resources :adverts) but, this seems to defeat some of the point of
using a REST structure in the first place. I’ve also seen people using
partials and passing in the name_prefix to them, but that seems pretty
inelegant to me (Restful polymorphic - Rails - Ruby-Forum)
I have to assume there must be a better way to generate the *path
functions, as this seems like a big open questions. Does anyone have any
advice?
Thanks,
Leslie