Forum: Ruby on Rails RESTful routes - nested AND direct?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5e75f4b81923cd04a5c8fffa290be92e?d=identicon&s=25 Carl Johnson (carlivar)
on 2007-01-08 07:20
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?
5e75f4b81923cd04a5c8fffa290be92e?d=identicon&s=25 Carl Johnson (carlivar)
on 2007-01-08 07:33
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.
F1e1789ff9e7fc3032fa6cf15382a1e8?d=identicon&s=25 Juanma Cervera (jmcervera)
on 2007-01-08 07:46
Carl Johnson 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
This topic is locked and can not be replied to.