How can I redirect a user's home (root) path based on their role using Devise?

后端 未结 5 2060
天涯浪人
天涯浪人 2020-12-02 11:18

I\'m working on a project management app, and in the app, I have project_managers and clients. I\'m using Devise and CanCan for authenticat

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 11:57

    Another option is to pass a proc to the authenticated method like this (I'm using rolify in this example):

    authenticated :user, ->(u) { u.has_role?(:manager) } do
      root to: "managers#index", as: :manager_root
    end
    
    authenticated :user, ->(u) { u.has_role?(:employee) } do
      root to: "employees#index", as: :employee_root
    end
    
    root to: "landing_page#index"
    

    Note that in Rails 4 you have to specify a unique name for each root route, see this issue for details.

提交回复
热议问题