1

コードは次のとおりです。

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

一見すると、管理者ロールを持つユーザーは、 のため、Web サイト コントローラーで何でもできるはずですcan :manage, :all

しかし、ウェブサイト/インデックスにアクセスすると、

CanCan::AccessDenied in WebsitesController#index

You are not authorized to access this page.

なぜこうなった?

4

1 に答える 1

1

私はとても愚かです。

user.role?は戻ってきますtrueが、true == "admin"常に偽です。

于 2013-03-28T00:50:50.993 に答える