polymorphic_url and namespaces

Sven Fuchs
Fri, 09 May 2008 14:53:22 -0000

So, this has been discussed in great length before, see [1], but to me
the solution [2] feels quite incomplete, so I thought I''d bring this
up again.

Currently polymorphic_url needs to be passed a namespace explicitely
to have it delegate to a namespaced url generation method:

   map.resources :articles
   map.namespace :admin do |admin|
     admin.resources :articles
   end

Now, no matter where link_to(@article) is called it always will
delegate to article_path.

 From my perspective it would make a lot of sense to have
polymorphic_url to

- delegate to article_path when we''re outside of the namespace but
- delegate to admin_article_path when we''re inside of the namespace

It has been argued in [1] that polymorphic_url just does its "best
guess" and thus the current behaviour was correct at that time.

But I find it hard to subscribe to that. With resources controllers,
views, routes etc. obviously are grouped and conceptually belong
together, so the "best guess" for link_to(@article) from within (e.g.)
views/admin/show would IMO to delegate to admin_article_path, not
article_path. Put differently I think that these url_helpers & co
should default to the current resource (including the namespace) we''re
working with, not an more or less arbitrary resource like it''s the
current behaviour.

Now, there currently doesn''t seem to be way to determine the current
namespace in a controller (and thus in polymorphic_url). From looking
at routes building/recognition etc. it might not be too hard to pass
the :namespace option from the route definition to the route (while
building it) and then to the controller instance (after recognition).

Am I missing something obvious?


[1] http://www.ruby-forum.com/topic/111136
[2] http://dev.rubyonrails.org/ticket/8640