Devise role based routing

北城余情 提交于 2019-12-08 08:45:12

问题


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


来源:https://stackoverflow.com/questions/11053175/devise-role-based-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!