April 28, 2007
Older: RailsConf 2007
Newer: Lorem ispum...
Namespaces added to routes
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 Comments
Apr 29, 2007
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.
Apr 29, 2007
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 01, 2007
(That’s Changeset 6594, rather than 6954 ;)
May 01, 2007
@James – Fixed. So I’m a bit dyslexic I guess. :)
May 07, 2007
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 07, 2007
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 07, 2007
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_options
work 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::CourseController
and http://any.other.domain/courses toPublic::CourseController
.Of course, it’s possible this is only ever useful in our special case. :-)
May 08, 2007
@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 19, 2007
Graeme, do you have a link? I need this functionality in an app I’m building…
Jun 21, 2007
Sweet, just what i was looking for – thanks for posting this :)
Oct 03, 2007
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.
Oct 05, 2007
@Tom – I don’t know of any way (other than creating a plugin backport) but Rails 2.0 should be out before too long.
Mar 19, 2008
Hello!
I think this try.
Sorry, comments are closed for this article to ease the burden of pruning spam.