未定义的方法'角色?'对于用户

时间:2012-03-19 16:19:37

标签: ruby-on-rails heroku cancan

我有以下abilities型号:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user, not logged in.

    if user.role? == :admin
      can :manage, :all
    else
      can :read, :all
      end
      if user.role == "default"
        can :create, Homescreen
        end
        can :destroy, Homescreen do |homescreen|
          homescreen.try(:user) == user
      end
  end
end

我有以下用户模型:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  before_create :setup_default_role_for_new_users


  attr_accessible :email, :password, :password_confirmation, :remember_me
  has_many :homescreens

  ROLES =%w[admin default]
  private
  def setup_default_role_for_new_users
    if self.role.blank?
      self.role = "default"
    end
  end
end

当我在本地计算机上运行时,它运行正常。

但是,当我部署到heroku时,我收到以下错误:

GET URL.herokuapp.com dyno=web.1 queue=0 wait=0ms service=158ms status=500 bytes=643
   app/models/ability.rb:7:in `initialize'
 NoMethodError (undefined method `role?' for #<User:0x00000003b56050>):

 cache: [GET /] miss

 Started GET "/" for my-ipaddress at 2012-03-19 16:14:38 +0000

 NoMethodError (undefined method `role?' for #<User:0x00000003df5d88>):
   app/models/ability.rb:7:in `initialize'

 cache: [GET /] miss

我做错了什么?

1 个答案:

答案 0 :(得分:2)

我使用以下命令重新启动了heroku:

heroku restart && heroku ps:restart

然后工作正常。