I've been looking around, and I can't seem to find anything definitive
on whether or not you can coax Routes into omitting the collection name
when doing a nested resource; i.e.
map.resource :users do |users|
users.resources :pages # some other option here
end
to get:
/users/:user_id/:id
instead of
/users/:user_id/pages/:id
Is this functionality actually there? If not, *should* it be? The
use-case scenario I'm thinking of is having users add pages to their
site, so:
/users/1 # user 1 view, which presumably lists the pages the user has
/users/1/research # user 1's research page
/users/1/fun # user 1's fun page
I'm not really sure how this conform with the Rails philosophy...
Nick
Hi Nick,
to get:
/users/:user_id/:id
instead of
/users/:user_id/pages/:id
Is this functionality actually there? If not, *should* it be? The
use-case scenario I'm thinking of is having users add pages to their
site, so:
/users/1 # user 1 view, which presumably lists the pages the user has
/users/1/research # user 1's research page
/users/1/fun # user 1's fun page
This question belongs to the general Rails mailing list (cross-posted, please remove the core list from the recipients if you reply to this message), but here goes.
AFAIK you cannot do that with restful routes. However, you only need a single named route before the restful route declarations to get what you want:
map.user_page 'users/:user_id/:id', :controller => "pages", :action => "show"
map.resources :users do |users|
users.resources :pages
end
That way you can use user_page_path(:user_id => @user, :id => @page) to get to that shorter url. For all other actions, the restful routes are used.
//jarkko
Nick,
In the example you give it’s really more of an issue of building a route that works for what you’re doing. What you’re trying to accomplish isn’t a resource. A resource would be one controller that taxes the standard HTTP verbs and maps them to actions. You’re just looking for a custom route like:
map.connect ‘/users/:user_id/:page_name’, :controller => ‘pages’, :action => ‘show’, :requirements => { :user_id => /\d+/ }
You’ll need to make sure that doesn’t conflict with any other routes you already have. If you have any further questions it would be more appropriate to continue in the Rails mailing list.
-Martin