Ruby on rails 未定义的局部变量或方法'role';对于用户:

Ruby on rails 未定义的局部变量或方法'role';对于用户:,ruby-on-rails,declarative-authorization,Ruby On Rails,Declarative Authorization,当我试图访问我的任何视图时,总是会出现这种错误。我正在尝试设置去极化授权 class User < ActiveRecord::Base acts_as_authentic do |c| c.login_field = :username end ROLES = %w[admin moderator subscriber] has_and_belongs_to_many :channels has_many :channel_mods named_s

当我试图访问我的任何视图时,总是会出现这种错误。我正在尝试设置去极化授权

class User < ActiveRecord::Base

  acts_as_authentic do |c|
    c.login_field = :username
  end

  ROLES = %w[admin  moderator subscriber]

  has_and_belongs_to_many :channels
  has_many :channel_mods
  named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }

  def role_symbols
    role.map do |role|
      role.name.underscore.to_sym
    end
  end

  def roles=(roles)  
    self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum  
  end

  def roles  
    ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }  
  end

end
class用户“角色{u掩码&{2**roles.index(role.to_s)}>0”}
定义角色符号
role.map do | role|
role.name.下划线.to_sym
结束
结束
def角色=(角色)
self.roles_mask=(roles&roles).map{r | 2**roles.index(r)}.sum
结束
def角色
ROLES.reject{| r |((ROLES|u mask | 0)&2**ROLES.index(r)).zero}
结束
结束

role\u symbols正在调用role.map,但是role还没有定义

谢谢,我以为你可以用ruby来定义这个无序的东西?不过,role似乎在任何地方都没有定义。您的意思是使用角色(复数)吗?定义为def roles。我猜是的@复数,因为每个用户可以有多个卷。