RESTful routes - nested AND direct?


#1

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?


#2

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

http://dev.rubyonrails.org/ticket/5761

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


#3

Carl J. wrote:

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

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