Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Route to root of a namespace

#3853

In Rails 2.3.x, you could do the following:

map.namespace :admin do |admin|
  admin.root :controller => "admin"
end

It appears this functionality is missing in the Rails 3 router. Neither of the following match /admin:

namespace :admin do
  root :to => 'admin#index'
  match '' => 'admin#index'
end

Reported by James · February 4th, 2010 @ 06:41 PM

State: invalid
Milestone: none
Assigned to: josh josh
Importance: Low

Activity

  1. josh
    josh
    • State changed from new to invalid

    I didn't realize that worked, but thats not the purpose of "root". The original intent was to provide a convention "root_url" named route plugins, etc could depend on. It doesn't make sense in the nested context.

    February 5th, 2010 @ 03:00 PM

  2. James
    James

    Josh,

    Previously it provided the "admin_root_url" named route, which seems to me like a great convention as it kept everything (including the index) within the context of admin. Even if you do consider this an "invalid" request, how would you recommend defining the root of a namespace?

    James

    February 5th, 2010 @ 04:58 PM

  3. Mark Connell
    Mark Connell

    This does actually work in the new DSL, however you have to explicitly set the namespace in the :to

    In rails 2 if you had an Admin::Posts controller the following would have worked:

    # http://localhost:3000/admin
    map.namespace :admin do |admin|
      admin.root :controller => 'posts'
    end
    

    And the admin.root automagically picked up the namespace context. To do the same in the new routing, the following examples work:

    # http://localhost:3000/admin
    namespace :admin do
      root :to => "admin/posts#index"
    end
    
    # http://localhost:3000/admin
    namespace :admin do
      root :to => "Admin::Posts#index"
    end
    

    February 8th, 2010 @ 02:09 PM

  4. James
    James

    Is there any reason we should need to explicitly set the namespace in the :to when we're within the namespace block? Seems like it should know about the namespace context and avoid duplication.

    February 8th, 2010 @ 11:21 PM