Devise根据登录用户角色重定向到特定页面 [英] Devise Redirects to specific page based on User Role on Login

查看:146
本文介绍了Devise根据登录用户角色重定向到特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个可能的权限,一个用户在我的Rails应用程序,他们是 User.is_admin User.is_school User.is_security 。根据我的应用程序的性质,我需要为每个用户做一个单独的主屏幕,这些用户完全不同,我正在工作。我所遇到的问题与Devise在所有用户登录后如何自动重定向到root_path有关,无论我设置了哪些权限。

I have three possible permissions for a User to be in my Rails app, they are User.is_admin, User.is_school, and User.is_security. Based on the nature of my app I need to have a separate home screen for each of these users that do radically different things, which I have working. The problem that I'm having has to do with how Devise auto redirects to root_path after login for all users, regardless of the permissions I have set.

我生成了Devise会话控制器进入用户命名空间,我已经将其覆盖到默认的我的控制器,但现在当我尝试做一个重定向,根据条件许可,我得到一个 DoubleRenderError (显而易见的原因是,在创建会话时,Devise正在重定向到其他地方)。

I generated the Devise Sessions controllers into the Users namespace and I have overwritten it to default to my controller, but now when I try to do a redirect, based on the conditional permissions, I get a DoubleRenderError (The obvious reason being that Devise is redirecting elsewhere when creating the session).

我尝试将其作为after_action运行,甚至尝试覆盖 after_sign_in_path_for 方法,根据Devise文档的方向,但我仍然无法使其工作。任何帮助将不胜感激,谢谢!

I have tried running it as an after_action and even tried overwriting the after_sign_in_path_for method, as per the direction of the Devise docs on the matter, but I still can't get it working. Any help would be appreciated, thank you!

推荐答案

你可以做这样的事情

class ApplicationController < ActionController::Base
  def after_sign_in_path_for(resource)
    if resource.role == 'admin'
      admin_root_path
    else
      user_root_path
    end
  end
end

您可以阅读更多关于这个 https://github.com/plataformatec/ devise / wiki / How-To:-redirect-to-a-specific-page-on-success-sign-in

you can read more about this https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in

这篇关于Devise根据登录用户角色重定向到特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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