# Nested Routes Best Practices

**URL:** <https://www.ruby-forum.com/t/nested-routes-best-practices/175465>\
**Category:** Rails\
**Created:** [September 14, 2009, 11:19am UTC](https://www.ruby-forum.com/t/nested-routes-best-practices/175465 "2009-09-14T11:19:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [September 14, 2009, 11:19am UTC](https://www.ruby-forum.com/t/nested-routes-best-practices/175465/1 "2009-09-14T11:19:01Z")

</div>

In a scaffold generated models, I have: List has\_many Items. I then  
modify the routing:

map.resources :lists, :shallow =\> true, :has\_many =\> :items  
map.resources :items, :only =\> [:index] # thought was that admin may  
need look at just the items.

There will be a route generated:

list\_items GET /lists/:list\_id/items  
(.:format)  
{:action=\>“index”, :controller=\>“items”}

If I enter a url " localhost:3000/lists/2/items" it will call the  
index action of the items controller with :list\_id in the parameters,  
but the generated default will list all items.

This is a good and bad thing. My question is: If I do this type of  
routing, am I supposed to add?

if params[:list\_id]  
do something  
else  
@items = Item.all  
end

It’s a good thing that I can do this because I can easily get related  
resources in an ajax call. Bad in that if I forget to put something  
like above in the nested controller (or restrict access some other  
way), the user has access to all nested resources.

Just have not seen this mentioned anywhere - but there are a lot of  
anywhere’s!

Steve A.

---

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [September 14, 2009, 1:51pm UTC](https://www.ruby-forum.com/t/nested-routes-best-practices/175465/2 "2009-09-14T13:51:53Z")

</div>

Hi Steve A.

I cant understand why you need

map.resources :items, :only =\> [:index]

```
 since item always coexist with lists So why it treated as seperate? 

```

Cant you do like

map.resources :lists, :shallow =\> true, :has\_many =\> :items

And then in index of items as

def index  
@list\_items = list.items  
end

def list  
@list = List.find(params[:list\_id])  
end

Sijo

---

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [September 15, 2009, 1:20pm UTC](https://www.ruby-forum.com/t/nested-routes-best-practices/175465/3 "2009-09-15T13:20:17Z")

</div>

You are right. I think I just added the :only =\> [:index] to test the  
concept. I was trying to point out that even with:

map.resources :lists, :shallow =\> true, :has\_many =\> :items

The index action is called and you must handle the situation that this  
only applies if :item\_id is present.

In a more complex scenario, I posted a reply in the thread \<http://  
[groups.google.com/group/rubyonrails-talk/browse\_thread/thread/](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/)  
d04c347caccc544a\> that points out that you must handle the index  
action based on the circumstance.

I may want to enter a nested route at some other point than the root  
(company in the example). Project managers may want to enter nested  
resources from a list of their current/active projects, or the boss  
may want to see all current/active projects. Learning the RoR way of  
defining these requirements has been a challenge. In some cases it  
provides an elegant simple solution. In others it adds things you  
didn’t expect.

Steve A.
