Forum: Ruby on Rails using *_path and *_url functions with nested resources

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Leslie F. (Guest)
on 2007-03-26 00:20
Hi all,
I've been scouring the web trying to find an answer to this problem, but
most of the discussions I've found seem old, inconclusive or both.

I'm trying to create a resource(REST) based app that has users and
adverts posted to categories. I'd like to have my routes set up like

map.resources :users do |users|
    users.resources :adverts, :name_prefix => "users_"

map.resources :categories do | categories |
    categories.resources :adverts, :name_prefix => "categories_"

Which would allow me to use URL like:

Which is all fine. However, in my adverts views, I need to generate the
paths to the actions in the correct parent resource. Obviously, I can't
do something like user_advert_path(@user, @advert), as it will be
incorrect when I render the view from a category parent.

I know I could do a workaround by having a base route to /adverts
(map.resources :adverts) but, this seems to defeat some of the point of
using a REST structure in the first place. I've also seen people using
partials and passing in the name_prefix to them, but that seems pretty
inelegant to me (

I have to assume there must be a better way to generate the *path
functions, as this seems like a big open questions. Does anyone have any

John W. (Guest)
on 2007-03-26 12:52
(Received via mailing list)
adverts_url(:user_id => 1) should generate


adverts_url(:user_id => 1, :id => 99) should generate


Use the same params for the *_path functions.


On Mar 25, 9:20 pm, Leslie F. <removed_email_address@domain.invalid>
Ben J. (Guest)
on 2007-03-27 06:10
The thing about shared partials is that they are not supposed to make
assumptions and should be oblivious to their environment. That being
said. Having URLs in your partials violates this rule in my opinion. If
there is anyway to extract that environment specific information you
should. I encounter this problem all the time. I've done extremely
complex forms, with all kinds of AJAX calls, javascript, etc built in. I
then had to turn around and use this form in another part of my
application. Using the same partial seemed dirty and was not right. I
just copy and pasted the form in the other section. Redundancy in your
views is not that bad of a thing. In fact I think redundancy in your
controllers and views is perfectly fine if being DRY gets in the way of
This topic is locked and can not be replied to.