# RESTful routes - nested AND direct?

**URL:** <https://www.ruby-forum.com/t/restful-routes-nested-and-direct/81354>\
**Category:** Rails\
**Created:** [January 8, 2007, 7:20am UTC](https://www.ruby-forum.com/t/restful-routes-nested-and-direct/81354 "2007-01-08T07:20:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [January 8, 2007, 7:20am UTC](https://www.ruby-forum.com/t/restful-routes-nested-and-direct/81354/1 "2007-01-08T07:20:17Z")

</div>

I’m switching over to RESTful routing and was looking forward to using  
the nested routing to make things a lot more convenient. However I’ve  
found that I don’t seem to be able to use both nested routes AND  
“direct” RESTful routes on the same controller.

For example, I would like to have both of these URLs:

/invoice/1

/user/5/invoice/1

Actually, the above seems to work from a routing standpoint. Using  
\*\_path to construct my links does not seem to work.

If I have:

map.resources :invoice

map.resources :user do |user|  
user.resources invoice  
end

…then this will not work:

invoice\_path(invoice)

It constructs a path like “/user/1/invoice”.

If I change the order in routes.rb and do the nested route first, the  
direct URL is correct but I can’t specify a parent any longer in \*\_path.

Any ideas?

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [January 8, 2007, 7:33am UTC](https://www.ruby-forum.com/t/restful-routes-nested-and-direct/81354/2 "2007-01-08T07:33:59Z")

</div>

Sorry for the reply to myself, but I found a solution:

[http://dev.rubyonrails.org/ticket/5761](http://dev.rubyonrails.org/ticket/5761)

Basically, :name\_prefix in the nested route definition allows me to  
differentiate between direct & nested in \*\_path.

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [January 8, 2007, 7:46am UTC](https://www.ruby-forum.com/t/restful-routes-nested-and-direct/81354/3 "2007-01-08T07:46:33Z")

</div>

Carl J. wrote:

> Sorry for the reply to myself, but I found a solution:
> 
> [http://dev.rubyonrails.org/ticket/5761](http://dev.rubyonrails.org/ticket/5761)
> 
> Basically, :name\_prefix in the nested route definition allows me to  
> differentiate between direct & nested in \*\_path.

Yes, this solves your question, but how can you manage to have only one  
controller using correctly the named routes in the views. Two sets of  
named routes and only one set of views, if you want stay DRY.

This is the problem I am questioning in a similar thread (Single Table  
Inheritance and RESTful routes), If you find a solution, please, I will  
thank you if you let me know.

Juan M. Cervera
