Nested resource controller names

3 views
Skip to first unread message

Andrew White

unread,
Aug 27, 2007, 3:12:27 AM8/27/07
to rubyonra...@googlegroups.com
What's the rationale behind the controller names of nested resources?

e.g:

map.resources :projects do |project|
project.resources :images
end

map.resources :galleries do |gallery|
gallery.resources :images
end

This results in the urls for /projects/:id/images and /galleries/:id/
images both going to the index action on the images controller. Even
if I were using a common image model and polymorphic associations I'd
thought it'd still be better to go to a project_images and
gallery_images controller otherwise you need to disambiguate whether
you need to load a project or gallery.

I know I can override the controller (extra typing) or change the
routing definition to :project_images (ugly urls), etc. But it seems
to me that the most common usage scenario would have the controller
prefixed with the singular name of the parent.

I have a patch which changes the behavior to this, but if it's 'Not
The Right Way' please can someone enlighten me


Andrew White

DHH

unread,
Aug 27, 2007, 10:25:28 PM8/27/07
to Ruby on Rails: Core
> What's the rationale behind the controller names of nested resources?
>
> e.g:
>
> map.resources :projects do |project|
> project.resources :images
> end
>
> map.resources :galleries do |gallery|
> gallery.resources :images
> end
>
> This results in the urls for /projects/:id/images and /galleries/:id/
> images both going to the index action on the images controller. Even
> if I were using a common image model and polymorphic associations I'd
> thought it'd still be better to go to a project_images and
> gallery_images controller otherwise you need to disambiguate whether
> you need to load a project or gallery.

That's by design. You can use the presence of gallery_id or project_id
in params to disambiguate where the request is coming from. I'm using
this all over Highrise.

> I know I can override the controller (extra typing) or change the
> routing definition to :project_images (ugly urls), etc. But it seems
> to me that the most common usage scenario would have the controller
> prefixed with the singular name of the parent.

That's not the usage pattern I've observed. I've wanted to use the
same controller for all the nested resources I've encountered. I don't
think having to specify the controller name is undue hardship in the
case that this is not desired.

Andrew White

unread,
Aug 28, 2007, 1:27:10 AM8/28/07
to rubyonra...@googlegroups.com

On 28 Aug 2007, at 03:25, DHH wrote:

>> I know I can override the controller (extra typing) or change the
>> routing definition to :project_images (ugly urls), etc. But it seems
>> to me that the most common usage scenario would have the controller
>> prefixed with the singular name of the parent.
>
> That's not the usage pattern I've observed. I've wanted to use the
> same controller for all the nested resources I've encountered. I don't
> think having to specify the controller name is undue hardship in the
> case that this is not desired.

Thanks for the clarification.

I'm guessing that the namespace option should be applied to nested
resources - edge currently only applies it to associations. I've
created a ticket (9399) with a patch to fix this.


Andrew White

DHH

unread,
Sep 10, 2007, 10:31:57 AM9/10/07
to Ruby on Rails: Core
> I'm guessing that the namespace option should be applied to nested
> resources - edge currently only applies it to associations. I've
> created a ticket (9399) with a patch to fix this.

Applied, thanks!

Reply all
Reply to author
Forward
0 new messages