1

My Rails 5 app uses Devise 4.2 to handle authentication for Admin and Agent user types (separate tables). It's all working, except for having the root be different for a logged in Admin vs Agent

In routes.rb I can set authenticated_root for either :admin OR :user, but doing both (as shown below) causes the error when the app is started: ArgumentError: Invalid route name, already in use: 'authenticated_root'

It seems Rails is ignoring that the two uses of authenticated_root occur in two different scenarios.

# routes.rb
devise_for :agents, :skip => [:registrations]
as :agent do
  get 'agents/edit' => 'devise/registrations#edit', :as => 'edit_agent_registration'    
  put 'agents' => 'devise/registrations#update', :as => 'agent_registration'            
end  

devise_for :admins, :skip => [:registrations]  
as :admin do
  get 'admins/edit' => 'devise/registrations#edit', :as => 'edit_admin_registration'    
  put 'admins' => 'devise/registrations#update', :as => 'admin_registration'            
end  

authenticated :admin do
  root 'pages#adminhome', as: :authenticated_root
end
authenticated :agent do
  root 'pages#agenthome', as: :authenticated_root
end

Should I perhaps be using some sort of lambda to set authenticated_root instead of the normal method documented, and used above?

I tried this approach instead but got the same error:

root :to => 'pages#adminhome', :constraints => lambda { |request| request.env['warden'].user.class.name == 'Admin' }, as: :authenticated_root
root :to => 'pages#agenthome', :constraints => lambda { |request| request.env['warden'].user.class.name == 'Agent' }, as: :authenticated_root
jpw
  • 18,697
  • 25
  • 111
  • 187

1 Answers1

1

I removed the "as: :authenticated_root" part and it works fine. I'm on rails 5.0.2.

The link below says the "as: :authenticated_root" part is necessary only because rails 4 doesn't allow routes with same name, assuming you already have a root route defined.

Different '/' root path for users depending if they are authenticated (using devise)

Community
  • 1
  • 1
user2576537
  • 791
  • 8
  • 4