设计重定向到自定义登录页面(常规和管理部分)(Devise redirect to custom login page (Regular and Admin Section))

编程入门 行业动态 更新时间:2024-10-26 17:28:55
设计重定向到自定义登录页面(常规和管理部分)(Devise redirect to custom login page (Regular and Admin Section))

我已经开始使用Devise进行自定义系统的Rails项目。 但在我的场景中,还有一个管理部分。 因此,管理员和普通用户都使用由Devise创建的“用户”模型。 我将“is_admin”字段添加到带有迁移的Model中。 但这次我有两个主要问题。

首先,我需要在admin部分有一个登录页面,它将使用Devise的登录方法,如果用户自动验证并查找“is_admin”字段以将用户重定向到管理页面。

第二个是,应该有常规站点的另一个登录页面,人们会看到。 这不会查找“is_admin”字段,而是使用常规的Devise登录表单。

那么这些可能与Devise有关吗?

谢谢

I have started a Rails project with Devise for authantication system. But in my scenerio, there is also an admin section. So both admin and regular users use the "User" model, which was created by Devise. I added "is_admin" field to Model with migrations. But this time i have 2 main problems.

First one is, i need to have a login page in admin section, which will use the Devise's login methods and if the user authanticate and look for the "is_admin" field to redirect user to admin pages.

Second one is, there should be another login page for the regular site, which people will see. This will not look for the "is_admin" field and will use regular Devise login forms.

So are these possible with Devise?

Thanks

最满意答案

在应用程序控制器中放置一个after_sign_in函数,如下所示,它将检查用户角色,然后重定向

class ApplicationController < ActionController::Base def after_sign_in_path_for(resource) if resource.is_admin? redirect_to admin_path else redirect_to user_path end end end

In the application controller place an after_sign_in function as below that would check for the user role and then redirect

class ApplicationController < ActionController::Base def after_sign_in_path_for(resource) if resource.is_admin? redirect_to admin_path else redirect_to user_path end end end

更多推荐

Devise,is_admin,admin,login,使用,电脑培训,计算机培训,IT培训"/> <meta name=&quo

本文发布于:2023-08-06 12:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   常规   重定向   页面   Devise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!