使用cancan获取未定义的局部变量或方法`roles' [英] using cancan getting undefined local variable or method `roles'
问题描述
load_and_authorize_resource
用户模型中的
def role? (角色)
roles.include? role.to_s
end
在能力模型中:
如果user.role? :superadmin
可以:管理,:全部
结束
但是,我得到以下错误:
未定义的局部变量或方法`roles'
app / models / user.rb:33 :在`角色'
app / models / ability.rb:7:在'initialize'
感谢您的帮助。
更新:由于下面的Bohdan的回答,我进一步了解文档,发现有不同的方法来设置cancan模型)。目前我们有6个不同的角色,导致数据库中有6个不同的布尔字段。我正在考虑一种层次化的方法来定义角色,其中一个用户可以拥有许多角色,一个角色有许多用户。有两种方法来设置角色定义。 第一个。 第二个。为了方便使用,我想我会详细定义每个角色,所以每个人只有一个角色。想知道这些的缺点是什么。
更新:除了上面定义的superadmin之外,我已经注释掉了所有其他角色。意识到它与许多问题无关。那么...?
你应该有 has_and_belongs_to_many:roles
您的模型或方法角色的任何其他自定义定义
修改
在您将 has_and_belongs_to_many:roles
添加到您的用户
模型中以使所有内容您需要的工作
定义新模型称为角色
至少包含名称
属性
更改
def role?(role)
roles.include? role.to_s
end
to
$角色扮演角色扮演角色扮演角色扮演角色扮演角色扮演角色扮演角色扮演角色扮演角色扮演角色扮演角色扮演角色扮演角色扮演角色扮演角色role.to_s
end
修改
迁移
类CreateRolesUsers< ActiveRecord :: Migration
def self.up
create_table:roles_users do | t |
t.integer:user_id
t.integer:role_id
end
end
def self.down
drop_table:roles_users
end
end
只需添加此迁移并运行 rake db :迁移
rails将执行其他操作
I've given up on trying to lock down every action in the application. Currently I'm placing in every controller except the devise/registration:
load_and_authorize_resource
in the user model:
def role?(role)
roles.include? role.to_s
end
in the ability model:
if user.role? :superadmin
can :manage, :all
end
However, I am getting the following error:
undefined local variable or method `roles'
app/models/user.rb:33:in `role?'
app/models/ability.rb:7:in `initialize'
Thanks for your help.
UPDATE: Because of Bohdan's answer below i looked further into the documentation and found there are differing methods of setting up the cancan model(s). currently we have 6 different roles resulting in 6 different Boolean fields in the database. I was thinking of a hierarchical approach to defining roles where one user could have many roles and one role has many users. There are two ways to set up the role definitions. First. Second. For ease of use i think i'll define each role exhaustively so there is only one role for each person. Wondering what the disadvantages of that are.
UPDATE: I commented out all the other roles other than superadmin as defined above. Realized that it doesn't have anything to do with many to many issue. So...?
You should have has_and_belongs_to_many :roles
in your model or any other custom definition for method roles
Edit
after you added has_and_belongs_to_many :roles
to your User
model to make everything work you need
define new model called Role
with at least name
attribute
change
def role?(role)
roles.include? role.to_s
end
to
def role?(role)
roles.map(&:name).include? role.to_s
end
Edit
migration
class CreateRolesUsers < ActiveRecord::Migration
def self.up
create_table :roles_users do |t|
t.integer :user_id
t.integer :role_id
end
end
def self.down
drop_table :roles_users
end
end
just add this migration and run rake db:migrate
rails will do the rest
这篇关于使用cancan获取未定义的局部变量或方法`roles'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!