在Rails控制台中绕过Cancan角色验证

时间:2012-09-11 14:44:54

标签: ruby-on-rails ruby ruby-on-rails-3

我创建了一个帮助器,它检查current_user的角色,以保护某些属性不被未经授权的用户编辑。

除了我在控制台中时,验证工作正常。显然没有用户,但它仍在尝试验证,我得到:

 NoMethodError: undefined method `has_role?' for nil:NilClass

如何绕过此验证或者有更好的方法来实现我正在做的事情?

位置模型:

  validate :check_archived_status, :on => :update

  def check_archived_status
    unless UserHelper.staff
      if self.archived == false
        if stuff == otherstuff
          do some other stuff
        end
      end
    end    
  end

UserHelper:

def self.staff
  staff = User.current_user.has_role?('Super', 'Admin', 'Tech')
end

1 个答案:

答案 0 :(得分:0)

您无法在控制台上使用随机用户填充current_user吗? 像这样:current_user = User.last,因此验证不会给你任何错误。