Bad routing or rails bug?

151 Views Asked by At

I have setup my routes into namespaces, so it look like

root to: "home#index"

namespace :users do
  root to: "profile#index"
  resources :registrations
  resources :sessions
end

namespace :admin do
  root to: "base#index"
end

rake routes |grep root
                root          /                               home#index
          admin_root          /admin(.:format)                admin/base#index
          users_root          /users(.:format)                users/profile#index

In my header navigation, I have = link_to "home", root_path

Everything work great in development mode, but completely broken in production

I'm getting No route matches {:controller=>"users/home"} when trying to access sessions/registrations controller (users/sessions/new)

the root_path in my header try to get home controller in users namespace

thanks in advance

2

There are 2 best solutions below

0
On

Distinguish the root path for each , and try it like

root to: "home#index" , :as => home_root

namespace :users do
  root to: "profile#index" , :as => users_root
  resources :registrations
  resources :sessions
end

namespace :admin do
  root to: "base#index" , :as => admin_root
end

use path like : home_root_path, users_root_path, admin_root_path

0
On

There is no home controller in the users namespace, there is a profile controller in the users namespace.

You need users_root_path to get to "users/profile#index".

But you're right, I would expect root_path to go to "home#index".