Rails 设计:验证两个模型中的任一个
Rails Devise: authenticate either two models
我正在构建 Rails 具有三种设计模型的应用程序:管理员、教师、学生。
我想创建一个管理员和教师可以访问的页面。
我知道如何创建一个角色的用户可以访问的页面。喜欢
before_action :authenticate_model!
但是我想查看管理员或老师的身份验证怎么办?
自动重定向路径可以固定一个。例如,如果用户在未登录的情况下访问了该页面,则该用户将被重定向到带有管理员超链接的教师登录页面。
有什么想法吗?
我想你可以使用 devise_group 选项。
示例:
inside BlogsController (or any other controller, it doesn't matter which):
devise_group :blogger, contains: [:user, :admin]
Generated methods:
authenticate_blogger! # Redirects unless user or admin are signed in
blogger_signed_in? # Checks whether there is either a user or an admin signed in
current_blogger # Currently signed in user or admin
current_bloggers # Currently signed in user and admin
Use:
before_action :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
before_action ->{ authenticate_blogger! :admin } # Redirects to the admin login page
current_blogger :user # Preferably returns a User if one is signed in
我正在构建 Rails 具有三种设计模型的应用程序:管理员、教师、学生。 我想创建一个管理员和教师可以访问的页面。
我知道如何创建一个角色的用户可以访问的页面。喜欢
before_action :authenticate_model!
但是我想查看管理员或老师的身份验证怎么办?
自动重定向路径可以固定一个。例如,如果用户在未登录的情况下访问了该页面,则该用户将被重定向到带有管理员超链接的教师登录页面。
有什么想法吗?
我想你可以使用 devise_group 选项。
示例:
inside BlogsController (or any other controller, it doesn't matter which):
devise_group :blogger, contains: [:user, :admin]
Generated methods:
authenticate_blogger! # Redirects unless user or admin are signed in
blogger_signed_in? # Checks whether there is either a user or an admin signed in
current_blogger # Currently signed in user or admin
current_bloggers # Currently signed in user and admin
Use:
before_action :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
before_action ->{ authenticate_blogger! :admin } # Redirects to the admin login page
current_blogger :user # Preferably returns a User if one is signed in