Lori Holden

Default Routes in Rails Namespaces

At YPC we have a default route that reads something like this:

map.connect '*path', :controller => 'exception', :action => 'issue_404'

Which basically means... we do not have a default route that connects unknown url's to controllers/actions.

Recently however, we needed some way to automatically handle routing to controllers that are, in effect, dynamically added. So we have URL's that look like this:

http://yellowpages.com/blah/things/controller/action

The first thing we thought to try was:

map.namespace(:blah) do |tool|
  tool.namespace(:things) do |thing|
    thing.connect '/:controller/:action'
  end
end

Makes sense right? To our surprise, rails would prefix /blah/things to ALL routes now... wha?

The next thing we did was trying to just remove the slash... thing.connect ':controller/:action'.

Which was the right track... our other url's are working properly again, but url's for /blah/things/:controller/:action just don't work.

I had assumed we where just running into a bug with the rails router code, but it looks like the namespace support in rails just has no concept of defaults in namespaces.

Enter my patch to add default routes to namespaces in rails.

Edit: It's been a long time since this post. The links no longer exist and have been removed.

With this change, you can add default routes to namespaces:

map.namespace(:blah) do |tool|
  tool.namespace(:things) do |thing|
    thing.connect ':controller/:action'
  end
end

Wasn't able to get it into the rails 2.2 release as its entering a code freeze, but hopefully it will make 2.3.