问题描述
我使用 RailsApps rails-composer 创建了一个 rails-devise-pundit 入门应用程序.我对 ruby on rails 还是有点陌生,对设计、专家和 rails 4 来说也比较新.
I used RailsApps rails-composer to create a rails-devise-pundit starter application. I am still a little new to ruby on rails and newer to devise, pundit and rails 4.
我正在查看代码以了解其工作原理.在控制器和策略类中有很多地方 user.admin?叫做.但是我找不到管理员?方法.我希望它在 User 模型中,但它不在那里.这是用户类:
I was looking at the code to learn how it works. There are many places in controllers and in policy classes where user.admin? is called. But I can't find the admin? method. I would expect it to be in the User model but it isn't there. Here's the user class:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
enum role: [:user, :vip, :admin]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :user
end
end
用于 users_controller.rb 的部分:
Used in part of users_controller.rb:
def show
@user = User.find(params[:id])
unless current_user.admin?
unless @user == current_user
redirect_to root_path, :alert => "Access denied."
end
end
end
pundit 或 devise 是否以某种方式创建了这种方法?我在权威文档中看到过它,但它只是将其用作示例.它没有说需要创建方法或处理它.是否以某种方式使用具有 :admin 作为可能值的角色枚举?如果有人可以解释这是如何工作的,我将不胜感激.
Does pundit or devise create this method somehow? I've seen it used in the pundit documentation but it just uses it as an example. It doesn't say the method needs to be created or that it handles it. Is it somehow using the role enum which has :admin as a possible value? If anyone can explain how this works, I'd appreciate it.
我打算很快添加使用 rolify gem 来处理角色而不是用户类中的角色枚举.也许我会出于某种原因想要制作完全不同的角色名称.我想确保我了解如何让一切正常工作.谢谢.
I'm planning to add in use of the rolify gem soon to handle roles instead of the role enum in the user class. Maybe I'll want to make role names that are completely different for some reason. I want to make sure I understand how to keep everything working. Thanks.
推荐答案
角色在 app/models/User.rb 文件(User
模型)中定义.
Roles are defined in the app/models/User.rb file (the User
model).
class User < ActiveRecord::Base
.
.
.
enum role: [:user, :vip, :admin]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :user
end
end
应用程序使用 ActiveRecord enum
方法来管理角色.ActiveRecord 提供了方便的方法来查询角色属性:
The application uses the ActiveRecord enum
method to manage roles. ActiveRecord provides convenient methods to query the role attribute:
user.admin! # sets the role to "admin"
user.admin? # => true
user.role # => "admin"
有关详细信息,请参阅 ActiveRecord::Enum 的文档.ActiveRecord enum
方法是 Rails 4.1 中的新方法.
See documentation for ActiveRecord::Enum for details. The ActiveRecord enum
method is new in Rails 4.1.
我已经更新了 rails-devise-pundit 应用程序的自述文件以包含此信息.我的Rails Pundit 教程中也介绍了这一点.
I've updated the README for the rails-devise-pundit application to include this information. It's also covered in my Rails Pundit Tutorial.
这篇关于user.admin 在哪里?在 rails-devise-pundit 入门应用程序中定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!