Namespaces added to routes
April 28th, 2007
Changeset 6594 just went through and it’s one that excites me. Most of the applications that I create require a front side and administration area. The administration area always requires authentication and the front side is typically wide open.
I’ve been using the principles of REST when creating both controllers, but haven’t found a way to take advantage of the sweetness that map.resource(s) provides. With the new change by DHH, it’s possible and I’m pretty happy with the syntax for doing so. To create an admin controller I can now doing something like:
map.namespace(:admin) do |admin|
admin.resources :products,
:collection => { :inventory => :get },
:member => { :duplicate => :post },
:has_many => [ :tags, :images, :variants ]
end
This will create admin_products_url, pointing to “admin/products”, which will look for an Admin::ProductsController. Also, because it lists the has_many relationships, it’ll also create admin_product_tags_url, pointing to “admin/products/#{product_id}/tags”, which will look for Admin::TagsController.
That is music to my ears. I can’t wait to refactor the controllers on a few of my current projects.
13 Responses to “Namespaces added to routes”
Sorry, comments are closed for this article to ease the burden of pruning spam. If you have any further comments, just send me an email.

April 29th, 2007 at 09:33 AM
Woah, freakout! I was just messing around in my routes.rb, trying to think of how to implement namespaces and maybe giving a plugin a shot. Will have to switch to edge to check this out.
April 29th, 2007 at 03:35 PM
I’ve been playing around with some Edge stuff, and much like you, have always needed both a front end and admin areas… this will definitely be something I will be checking out in the near future!
May 1st, 2007 at 10:11 AM
(That’s Changeset 6594, rather than 6954 ;)
May 1st, 2007 at 12:36 PM
@James – Fixed. So I’m a bit dyslexic I guess. :)
May 7th, 2007 at 01:46 AM
From a RESTful perspective, you’re not supposed to create a separate controller for your administrative purposes and for for wide-open front. They should be one and the same, or you’re providing duplication.
May 7th, 2007 at 02:34 AM
Stephen: That’s true, of course. But if public pages may be cached in an application, then having a separate admin controller (or at least actions) may be worth the price, I think :)
May 7th, 2007 at 02:56 AM
Ah well, there goes polishing up and publishing that plugin. :-)
Hmm, come to think of it, I might be doing something extra. I’m making
map.with_optionswork for resources, so that I can use Jamis’ routing_tricks and do something along the lines of:which maps http://editorial.some.domain/courses to
Editorial::CourseControllerand http://any.other.domain/courses toPublic::CourseController.Of course, it’s possible this is only ever useful in our special case. :-)
May 8th, 2007 at 10:12 AM
@Stephen – Most of the admin areas that I build are more for web sites than web applications. There is a difference between the two. I agree that web apps rarely need a separate area to manage but websites do. I have found that the users (content managers) seem to relate more to managing content on one side and viewing content on another. The separation seems to be easier to learn.
May 19th, 2007 at 11:48 PM
Graeme, do you have a link? I need this functionality in an app I’m building…
June 21st, 2007 at 02:25 PM
Sweet, just what i was looking for – thanks for posting this :)
October 3rd, 2007 at 05:29 PM
Is there a way to take advantage of this without going on Edge rails? I like the idea of continuing RESTful controllers while breaking up the namespace. Even if the routes look a little funky until 2.0, I would be interested in giving it a shot.
October 5th, 2007 at 11:11 AM
@Tom – I don’t know of any way (other than creating a plugin backport) but Rails 2.0 should be out before too long.
March 19th, 2008 at 11:22 AM
Hello! I think this try.