我怎么能基于使用设计自己的角色重定向用户的主(根)路径? [英] How can I redirect a user's home (root) path based on their role using Devise?

查看:111
本文介绍了我怎么能基于使用设计自己的角色重定向用户的主(根)路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目管理应用程序,并在应用程序中,我有 project_managers 客户即可。我使用的设计和惨惨认证/授权。

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 authentication/authorization.

在登录后,什么时候我应该将用户重定向到自己特定的控制器/布局/看法?有没有办法根据的routes.rb 检查 current_user.role ,并设置根(或重定向)不管他们是否是一个项目经理或客户端?这是一个变化,我可以在设计使地方?

At what point after login should I be redirecting the user to their own specific controller/layout/views? Is there a way to check for current_user.role in routes.rb and set the root (or redirect) based on whether or not they're a project manager or a client? Is this a change I can make in Devise somewhere?

在此先感谢您的帮助!
--mark

Thanks in advance for any help! --Mark

推荐答案

的routes.rb 文件将不会让用户有什么样的作用的想法,所以你赢了'T能够使用它来分配特定的根路径。

Your routes.rb file won't have any idea what role the user has, so you won't be able to use it to assign specific root routes.

你可以做的是建立一个控制器(例如, passthrough_controller.rb ),这反过来又可以读取角色和重定向。事情是这样的:

What you can do is set up a controller (for example, passthrough_controller.rb) which in turn can read the role and redirect. Something like this:

# passthrough_controller.rb
class PassthroughController < ApplicationController
  def index
    path = case current_user.role
      when 'project_manager'
        some_path
      when 'client'
        some_other_path
      else
        # If you want to raise an exception or have a default root for users without roles
    end

    redirect_to path     
  end
end

# routes.rb
root :to => 'passthrough#index'

这样,所有用户必须项,从而其重定向到相应的控制器/动作取决于他们的角色的一个点。

This way, all users will have one point of entry, which in turn redirects them to the appropriate controller/action depending on their role.

这篇关于我怎么能基于使用设计自己的角色重定向用户的主(根)路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆