CanCan + Devise:管理员用户即使应该也无法获得许可

时间:2013-03-28 00:27:07

标签: ruby-on-rails devise cancan

以下是代码。

class WebsitesController < ApplicationController
  load_and_authorize_resource

  ...
end
class ApplicationController < ActionController::Base
  ...

  check_authorization :unless => :do_not_check_authorization?

  private
    def do_not_check_authorization?
      respond_to?(:devise_controller?)
    end
end
class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    if user.role? == "admin"
      can :manage, :all
    elsif user.role? == "developer"
      can :manage, :websites
      can :manage, :pages
      can :manage, :templates
    elsif user.role? == "editor"
      can :manage, :pages
    end
  end
end

从它的角度来看,具有管理员角色的用户应该能够使用网站控制器做任何事情,因为can :manage, :all

但是当我点击网站/索引时,我得到了

CanCan::AccessDenied in WebsitesController#index

You are not authorized to access this page.

为什么会这样?

1 个答案:

答案 0 :(得分:1)

我太笨了。

user.role?会返回true,但true == "admin"始终为假。