On our TechCrunch Elevator Pitches site we have an admin interface that lives inside of a “admin” namespace. The route declaration in routes.rb looks like this:
map.namespace(:admin) do |admin| admin.resources :videos, :member => {:update_status => :put} admin.resources :comments admin.root :controller => "videos" end map.connect "logged_exceptions/:action/:id", :controller => "logged_exceptions"
We also use the Exception Logger plugin to track exceptions. So we have a route for that.
I ran into trouble in the admin/videos/index.html.erb view when I attempted to link outside the admin namespace to the logged_exceptions page. This is what I was trying to do that doesn’t work:
<%= link_to "Exceptions", :controller => "logged_exceptions" %>
The code above creates the following url: http://foo.com/admin/logged_exceptions. But what I needed is this: http://foo.com/logged_exceptions. This problem has not come up before because we normally use named routes which will resolve to the correct route regardless of the current namespace.
The solution is really simple but took me a while to figure out. To explicitly direct Rails to look for the controller at the site root place a “/” before the controller name. The correct link_to statement looks like:
<%= link_to "Exceptions", :controller => "/logged_exceptions" %>
Hopefully this helps anyone in the same situation.





Oh, I nearly forgot about that one. It might be smart to add this to the doc’s if it’s really the intended behavior. If you think of it.. Once you define a name-space, you just have to put *everything* in it’s name-space, so /action is basically a shorthand for root/action.
Thanks
Sadly, this would introduce another search and replace step (similar to renaming all the path helpers after nesting a resource). I believe these appearances of name-spaces where they don’t belong ought to be solved with a bit better pattern recognition (i.e.: If route belongs to no namespaces, give it to root). I tried to quickly wrap my head around it, but it seems somebody really had a blast with meta-programming Routing::Route#generate. Wild enough to keep me in the no-name-spaced-controllers camp for a few more releases. I didn’t get here without turning a few hair gray while trying to keep all the controller logic in nifty sub-folders though, so I really hope I remember this trick next time I work on any older projects
Thank you! That caused me a half hour of head scratching — a lot less than if you hadn’t wrote this blog post!