# Smarter routing with nested resources

**URL:** <https://www.ruby-forum.com/t/smarter-routing-with-nested-resources/125653>\
**Category:** Rails\
**Created:** [January 10, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/smarter-routing-with-nested-resources/125653 "2008-01-10T23:50:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [January 10, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/smarter-routing-with-nested-resources/125653/1 "2008-01-10T23:50:34Z")

</div>

I have a number of resources that I want to access in various scopes.  
The index action would change slightly depending on its scope, but the  
rest of the actions would be identical aside from needing to preserve  
the url scope in links and redirects.

For example:

map.resources :categories do |categories|  
categories.resources :products do |products|  
products.resources :components  
end  
end

map.resources :products do |products|  
products.resources :components  
end

map.resources :components

This enables the following routes all to work:

/categories/1/products/1/components/1  
/products/1/components/1  
/components/1

However, if I understand correctly, I have to specify which route in the  
named routes:

category\_product\_components\_path  
product\_components\_path  
components\_path

I’d like to be able to use components\_path and have it be smart enough  
based on the options and params to preserve as much scoped routing as  
possible. For example:

Calling components\_path in /categories/1/products/1 -\>  
/categories/1/products/1/components  
Calling components\_path in /products/1 -\> /products/1/components  
Calling components\_path somewhere else -\> /components

Is there any way to do this?

–  
Jack C.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [January 10, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/smarter-routing-with-nested-resources/125653/2 "2008-01-10T23:57:01Z")

</div>

category\_product\_components\_path is the only way afaik, without hacking  
up  
your own version of it.

Why would you need to have 3-level deep nested routes anyway?  
“/categories/1/products/1/components/1/” Really all you’re looking at  
here  
is the component, who cares what category and product it’s from, you can  
do  
that query in the controller.

On Jan 11, 2008 9:17 AM, Jack C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> products.resources :components  
> This enables the following routes all to work:  
> components\_path  
> Is there any way to do this?
> 
> –  
> Jack C.  
> removed\_email\_address@domain.invalid
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [January 11, 2008, 3:42am UTC](https://www.ruby-forum.com/t/smarter-routing-with-nested-resources/125653/3 "2008-01-11T03:42:19Z")

</div>

Ryan B. wrote:

> category\_product\_components\_path is the only way afaik, without hacking up your own version of it.  
>   
> Why would you need to have 3-level deep nested routes anyway? "/categories/1/products/1/components/1/" Really all you're looking at here is the component, who cares what category and product it's from, you can do that query in the controller.

For the components index page I need to know if I should display all components or only those that belong to the given product. For the actions like show, edit and update I need to preserve that state so when I go back to the index I go back to the right index page. The user will start on an index page at some level of scoping, he then goes to show a component. At this point I have the referer so I don't have a problem going back yet. Then if he goes from show to edit and update that component. At this point if I'm only at "/components/1" I have no way of knowing what index page I should go back to.  
  
In my production system I have many resources that have many relations (one model has ~30 associations). So there are are many places where I need an index of all records that belong to another, to be able to do multiple actions to an individual record, and finally go back to the original index page.  
  
I have used the session to to store where I should go back to, but that has its own nasty set of problems. It seems that this state should be kept in the URL.  

> On Jan 11, 2008 9:17 AM, Jack C. \<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
> 
> > I have a number of resources that I want to access in various scopes.  
> > The index action would change slightly depending on its scope, but the  
> > rest of the actions would be identical aside from needing to preserve  
> > the url scope in links and redirects.  
> >   
> > For example:  
> >   
> > map.resources :categories do |categories|  
> > &nbsp;categories.resources :products do |products|  
> > &nbsp; &nbsp;products.resources :components  
> > &nbsp;end  
> > end  
> >   
> > map.resources :products do |products|  
> > &nbsp;products.resources :components  
> > end  
> >   
> > map.resources :components  
> >   
> >   
> > This enables the following routes all to work:  
> >   
> > /categories/1/products/1/components/1  
> > /products/1/components/1   
> > /components/1  
> >   
> > However, if I understand correctly, I have to specify which route in the  
> > named routes:  
> >   
> > category\_product\_components\_path  
> > product\_components\_path  
> > components\_path  
> >   
> > I'd like to be able to use components\_path and have it be smart enough   
> > based on the options and params to preserve as much scoped routing as  
> > possible. For example:  
> >   
> > Calling components\_path in /categories/1/products/1 -\>  
> > /categories/1/products/1/components  
> > Calling components\_path in /products/1 -\> /products/1/components   
> > Calling components\_path somewhere else -\> /components  
> >   
> > Is there any way to do this?  
> >   
> > --  
> > Jack C.  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> >   
> >   
> > [http://www.frozenplague.net](http://www.frozenplague.net)  
> > Feel free to add me to MSN and/or GTalk as this email.

  
  

```
--
Jack C.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
```
  
--~--~---------~--~----~------------~-------~--~----~  
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.   
 To post to this group, send email to removed\_email\_address@domain.invalid   
 To unsubscribe from this group, send email to removed\_email\_address@domain.invalid   
 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en   
 -~----------~----~----~----~------~----~------~--~---

---

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [January 11, 2008, 4:02am UTC](https://www.ruby-forum.com/t/smarter-routing-with-nested-resources/125653/4 "2008-01-11T04:02:47Z")

</div>

Ok, well /products/1/components/1 will let you go back as far as you  
need to  
go back, right?

On Jan 11, 2008 1:00 PM, Jack C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> For the components index page I need to know if I should display all  
> (one model has ~30 associations). So there are are many places where I need
> 
> > end
> 
> –  
> Jack removed\_email\_address@domain.invalid
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [January 11, 2008, 4:36am UTC](https://www.ruby-forum.com/t/smarter-routing-with-nested-resources/125653/5 "2008-01-11T04:36:40Z")

</div>

Ryan B. wrote:

> Ok, well /products/1/components/1 will let you go back as far as you need to go back, right?

Correct, assuming I came through /categories/products/1. The problem is I may have just come through /products/1. I basically just need to keep whatever the prefix is before the /components. I have hacked together some custom url helpers in which I list all the possible entrance paths and the params that trigger that path. It more or less works, but in theory all the information I'm duplicating in those helpers exists in the routes somewhere. Maybe I should look into reflecting on routes.  

> On Jan 11, 2008 1:00 PM, Jack C. \<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
> 
> > Ryan B. wrote: 
> > 
> > > category\_product\_components\_path is the only way afaik, without hacking up your own version of it.  
> > >   
> > > Why would you need to have 3-level deep nested routes anyway? "/categories/1/products/1/components/1/" Really all you're looking at here is the component, who cares what category and product it's from, you can do that query in the controller.
> > 
> > For the components index page I need to know if I should display all components or only those that belong to the given product. For the actions like show, edit and update I need to preserve that state so when I go back to the index I go back to the right index page. The user will start on an index page at some level of scoping, he then goes to show a component. At this point I have the referer so I don't have a problem going back yet. Then if he goes from show to edit and update that component. At this point if I'm only at "/components/1" I have no way of knowing what index page I should go back to.  
> >   
> > In my production system I have many resources that have many relations (one model has ~30 associations). So there are are many places where I need an index of all records that belong to another, to be able to do multiple actions to an individual record, and finally go back to the original index page.  
> >   
> > I have used the session to to store where I should go back to, but that has its own nasty set of problems. It seems that this state should be kept in the URL.  
> > 
> > > On Jan 11, 2008 9:17 AM, Jack C. \<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
> > > 
> > > > I have a number of resources that I want to access in various scopes.  
> > > > The index action would change slightly depending on its scope, but the  
> > > > rest of the actions would be identical aside from needing to preserve  
> > > > the url scope in links and redirects.  
> > > >   
> > > > For example:  
> > > >   
> > > > map.resources :categories do |categories|  
> > > > &nbsp;categories.resources :products do |products|  
> > > > &nbsp; &nbsp;products.resources :components  
> > > > &nbsp;end  
> > > > end  
> > > >   
> > > > map.resources :products do |products|  
> > > > &nbsp;products.resources :components  
> > > > end  
> > > >   
> > > > map.resources :components  
> > > >   
> > > >   
> > > > This enables the following routes all to work:  
> > > >   
> > > > /categories/1/products/1/components/1  
> > > > /products/1/components/1   
> > > > /components/1  
> > > >   
> > > > However, if I understand correctly, I have to specify which route in the  
> > > > named routes:  
> > > >   
> > > > category\_product\_components\_path  
> > > > product\_components\_path  
> > > > components\_path  
> > > >   
> > > > I'd like to be able to use components\_path and have it be smart enough   
> > > > based on the options and params to preserve as much scoped routing as  
> > > > possible. For example:  
> > > >   
> > > > Calling components\_path in /categories/1/products/1 -\>  
> > > > /categories/1/products/1/components  
> > > > Calling components\_path in /products/1 -\> /products/1/components   
> > > > Calling components\_path somewhere else -\> /components  
> > > >   
> > > > Is there any way to do this?  
> > > >   
> > > > --  
> > > > Jack C.  
> > > > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > >   
> > > >   
> > > > 
> > > > [http://www.frozenplague.net](http://www.frozenplague.net)  
> > > > Feel free to add me to MSN and/or GTalk as this email.
> > 
> >   
> >   
> > 
> > ```
> > --
> > Jack C.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> > ```
> 
>   
>   
>   
> --   
> Ryan B.  
> [http://www.frozenplague.net](http://www.frozenplague.net)  
> Feel free to add me to MSN and/or GTalk as this email.

  
  

```
--
Jack C.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
```
  
--~--~---------~--~----~------------~-------~--~----~  
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.   
 To post to this group, send email to removed\_email\_address@domain.invalid   
 To unsubscribe from this group, send email to removed\_email\_address@domain.invalid   
 For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en   
 -~----------~----~----~----~------~----~------~--~---
