using cancan getting undefined local variable or method `roles'

时光总嘲笑我的痴心妄想 提交于 2019-12-08 10:02:44

问题


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


回答1:


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




回答2:


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

create_table :user_roles


来源:https://stackoverflow.com/questions/7021053/using-cancan-getting-undefined-local-variable-or-method-roles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!