Devise role based routing

2019-06-02 06:17发布

问题:

I have an app with multiple users. Each user as a theoretical role (user, client, etc). I've designed a view/controller for each user type.

I want to be able to login each type of user do a different root url and lock them to it.

Originally I was going to add a column to Users in Devise called role and so I can differentiate the users. The problem I'm having is how to say in routes.rb if current_user.role == "client" root :to => 'controller#index'

Once they are logged in to the page I also want to keep them from being able to visit any of my other paths ie: domain.com/calls domain.com/units

I've been looking into cancan to run alongside Devise but I'm not sure if this is the answer.

回答1:

Instead of handling it in routes, why not handle it in ApplicationController?

#application_controller.rb
before_filter :direct_to

def direct_to
  if current_user.role == "client"
    redirect_to client_controller_path
  # etc
end