使用 cancan 获取未定义的局部变量或方法“roles”;

发布于 2024-11-29 09:24:27 字数 913 浏览 0 评论 0原文

我已经放弃尝试锁定应用程序中的每个操作。目前,我正在放置除设计/注册之外的每个控制器:

load_and_authorize_resource

在用户模型中:

  def role?(role)
    roles.include? role.to_s
  end

在能力模型中:

if user.role? :superadmin
  can :manage, :all
end

但是,我收到以下错误:

undefined local variable or method `roles'
app/models/user.rb:33:in `role?'
app/models/ability.rb:7:in `initialize'

感谢您的帮助。

更新:由于 Bohdan 下面的回答,我进一步查看了文档,发现设置 cancan 模型有不同的方法。目前我们有 6 个不同的角色,导致数据库中有 6 个不同的布尔字段。我正在考虑一种分层方法来定义角色,其中一个用户可以拥有多个角色,一个角色可以拥有多个用户。有两种方法可以设置角色定义。 首先第二。为了便于使用,我想我将详尽地定义每个角色,这样每个人只有一个角色。想知道这样做的缺点是什么。

更新:我注释掉了除上面定义的超级管理员之外的所有其他角色。意识到这与多对多问题没有任何关系。所以...?

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...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裂开嘴轻声笑有多痛 2024-12-06 09:24:27

您的模型或方法 roles 的任何其他自定义定义中应该有 has_and_belongs_to_many

:roles 编辑

在添加 has_and_belongs_to_many :roles 到您的 User 模型,以使您需要的一切正常工作

定义名为 Role 的新模型,至少

更改

def role?(role)
  roles.include? role.to_s
end

name 属性要

def role?(role)
  roles.map(&:name).include? role.to_s
end

编辑

迁移,

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

只需添加此迁移并运行rake db:migraterails将完成剩下的工作

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

山川志 2024-12-06 09:24:27

Rails HBTM 关系按字母顺序对表名称进行排序。尝试将您的迁移更改为

create_table :user_roles

rails HBTM relations order the table name alphabetically. Try changing your migration to

create_table :user_roles
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文