from the desk of michael nutt

Rails routing and namespaces

A while back, Rails gained the ability to namespace routes.  For instance, if you wanted to add a blog to your app but didn't want to pollute its top-level URL space, you could do this:

namespace(:blog) do 
  map.resources :posts
  map.resources :tags
end

Then you get a nice blog at "/blog" and everything is great, right?  You now have to put your post blog controller in app/controllers/blog/posts_controller.rb, and put your views in app/view/blogs/posts/.  It seems clean, but it turns into so much typing that it gets annoying.  When rendering partials explicitly, you have to add the "blog/" prefix.  Worse, you now have to append "blog_" to every named route helper you use.

Namespaced do happen to be great for disambiguation, however.  Admin interfaces are a perfect example: you can put your exposed blog controller in app/controllers/blogs_controller.rb and put your admin blog controller in app/controllers/admin/blogs_controller.rb.

I made the mistake of namespacing a number of controllers because I thought it would make for a cleaner grouping, and I am paying for it in the time I have to take to de-namespace.  The moral of the story is to only use namespaced routes when :path_prefix => "/blog" won't work.

Comments

Loading Plugin
Loading Plugin
Loading Plugin
Loading Plugin
Loading Plugin

Valid HTML 4.01 Strict