用户的未定义局部变量或方法“role”:

时间:2010-11-28 00:27:28

标签: 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_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

1 个答案:

答案 0 :(得分:1)

role_symbols正在调用role.map,但尚未定义角色