# Nested map.resources without collection segment?

**URL:** <https://discuss.rubyonrails.org/t/nested-map-resources-without-collection-segment/5525>\
**Category:** rubyonrails-core\
**Created:** [December 6, 2006, 4:13pm UTC](https://discuss.rubyonrails.org/t/nested-map-resources-without-collection-segment/5525 "2006-12-06T16:13:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Howell](https://avatars.discourse-cdn.com/v4/letter/n/3d9bf3/32.png) [@Nick\_Howell](https://discuss.rubyonrails.org/u/Nick_Howell)\
**Post date:** [December 6, 2006, 4:13pm UTC](https://discuss.rubyonrails.org/t/nested-map-resources-without-collection-segment/5525/1 "2006-12-06T16:13:34Z")

</div>

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| &nbsp;&nbsp;users.resources :pages # some other option here end

to get: &nbsp;&nbsp;/users/:user\_id/:id instead of &nbsp;&nbsp;/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

---

<div class="post-metadata">

**Author:** ![Jarkko\_Laine](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jarkko\_Laine](https://discuss.rubyonrails.org/u/Jarkko_Laine)\
**Post date:** [December 6, 2006, 4:54pm UTC](https://discuss.rubyonrails.org/t/nested-map-resources-without-collection-segment/5525/2 "2006-12-06T16:54:51Z")

</div>

Hi Nick,

> to get: &nbsp;&nbsp;/users/:user\_id/:id instead of &nbsp;&nbsp;/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| &nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Martin\_Emde](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@Martin\_Emde](https://discuss.rubyonrails.org/u/Martin_Emde)\
**Post date:** [December 6, 2006, 4:58pm UTC](https://discuss.rubyonrails.org/t/nested-map-resources-without-collection-segment/5525/3 "2006-12-06T16:58:56Z")

</div>

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
