# Routes both nested and RESTful

**URL:** <https://www.ruby-forum.com/t/routes-both-nested-and-restful/116065>\
**Category:** Rails\
**Created:** [October 2, 2007, 12:48am UTC](https://www.ruby-forum.com/t/routes-both-nested-and-restful/116065 "2007-10-02T00:48:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![datorrey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cab0a1/32.png) [@datorrey](https://www.ruby-forum.com/u/datorrey)\
**Post date:** [October 2, 2007, 12:48am UTC](https://www.ruby-forum.com/t/routes-both-nested-and-restful/116065/1 "2007-10-02T00:48:26Z")

</div>

Most of the material on custom routes and URL generation in Rails  
assumes non-nested, non-RESTful routes it seems. Especially for  
non-crud actions it is difficult to find good examples for nested  
resources.

RESTful tutorials show the basic CRUD routes, and some examples, but  
scaffolding doesn’t generate example code that takes these routes into  
account.

Non RESTful url creation for example is easy:  
:url =\> { controller: controller\_name, :action =\> action\_name, :id =\> ID  
}

generates:  
controller\_name/action\_name/ID

But what about nested and restful resources like this:  
parent\_controller\_name/parent\_ID/controller\_name/ID;action\_name

How do I generate these? Do I have to construct and hard-code the URL  
path myself?

Also, the functional controller tests refer to a simple action like  
this:  
get :new

How do I handle calling the “new” action for a nested resource?

---

<div class="post-metadata">

**Author:** ![datorrey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cab0a1/32.png) [@datorrey](https://www.ruby-forum.com/u/datorrey)\
**Post date:** [October 2, 2007, 11:20am UTC](https://www.ruby-forum.com/t/routes-both-nested-and-restful/116065/2 "2007-10-02T11:20:11Z")

</div>

Daniel T. wrote:

> But what about nested and restful resources like this:  
> parent\_controller\_name/parent\_ID/controller\_name/ID;action\_name
> 
> How do I generate these? Do I have to construct and hard-code the URL  
> path myself?

In routes.rb you can define extra actions to go with your resource, and  
you’ll get those helpers for free. Look up the specifics on :collection  
and :member.

> Also, the functional controller tests refer to a simple action like  
> this:  
> get :new
> 
> How do I handle calling the “new” action for a nested resource?

You need to pass the IDs for the parent resources like so:

get :new, :parent\_id =\> 1

–  
Roderick van Domburg  
[http://www.nedforce.com](http://www.nedforce.com)

---

<div class="post-metadata">

**Author:** ![datorrey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cab0a1/32.png) [@datorrey](https://www.ruby-forum.com/u/datorrey)\
**Post date:** [October 2, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/routes-both-nested-and-restful/116065/3 "2007-10-02T14:04:11Z")

</div>

I’ll just show you an example. Let’s say you wanted to nest a city  
inside of a state, like:

[http://localhost/states/1/cities/3/](http://localhost/states/1/cities/3/)

To nest the resources, you need to…nest them…in routes. 🙂 Like  
this:

map.resources :states do |state|  
state.resources :cities  
end

Then, to add custom methods to each member (for example, if you wanted  
to show the population for a city or something), then you would do  
something like this:

map.resources :states do |state|  
state.resources :cities, :member =\> { :population =\> :get }  
end

…where population is the action name and get is the HTTP method you  
want to allow to be used on it. You could also do collection in the  
place of member to allow things like  
[http://localhost/states/3/cities/average\_population](http://localhost/states/3/cities/average_population) or something.

Keep in mind that when you nest resources, though, it also requires  
you call url\_for/link\_to with nesting. For example, rather than…

city\_path the\_city\_id

…you need to do something like this:

state\_city\_path the\_state\_id, the\_city\_id

This also applies to custom methods.

Hopefully that will give you enough to get going. 🙂

–Jeremy

On 10/1/07, Daniel T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Non RESTful url creation for example is easy:  
> path myself?

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

Ruby in Practice:

> **[Ruby in Practice](https://www.manning.com/books/ruby-in-practice)**
>
> Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale environments. A cookbook-style reference, it gives you concrete examples of systems...

My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![datorrey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cab0a1/32.png) [@datorrey](https://www.ruby-forum.com/u/datorrey)\
**Post date:** [October 2, 2007, 3:21pm UTC](https://www.ruby-forum.com/t/routes-both-nested-and-restful/116065/4 "2007-10-02T15:21:41Z")

</div>

On Oct 2, 4:20 am, Roderick van Domburg \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> You need to pass the IDs for the parent resources like so:
> 
> get :new, :parent\_id =\> 1

What about with namespaces where there is no :parent\_id? In my views,  
I would just pass :admin, but I’m not sure what to pass over in my  
tests. Maybe  
:admin =\> :admin  
or  
:admin =\> nil
