April 28, 2007

Posted by John

Tagged namespaces and routing

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

  1. 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.

  2. David Parker David Parker

    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!

  3. (That’s Changeset 6594, rather than 6954 ;)

  4. @James – Fixed. So I’m a bit dyslexic I guess. :)

  5. 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.

  6. Vladimir Bobes Tuzinsky Vladimir Bobes Tuzinsky

    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 :)

  7. 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:

      map.with_options :namespace => 'editorial', :conditions => { :subdomain => "editorial" } do |editorial|
        editorial.resources :courses do |course|
          course.resource :exam
        end
      end
    
      map.with_options :namespace => 'public' do |portal|
        portal.resources :courses do |course|
          course.resource :exam
        end
      end

    which maps http://editorial.some.domain/courses to Editorial::CourseController and http://any.other.domain/courses to Public::CourseController.

    Of course, it’s possible this is only ever useful in our special case. :-)

  8. @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.

  9. Graeme, do you have a link? I need this functionality in an app I’m building…

  10. Sweet, just what i was looking for – thanks for posting this :)

  11. Tom Rossi Tom Rossi

    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.

  12. @Tom – I don’t know of any way (other than creating a plugin backport) but Rails 2.0 should be out before too long.

  13. Floroskop Floroskop

    Mar 19, 2008

    Hello!
    I think this try.

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.

Projects

Flipper
Release your software more often with fewer problems.
Flip your features.