簡體   English   中英

cancancan load_and_authorize_resource NameError

[英]cancancan load_and_authorize_resource NameError

我使用CanCanCan,Devise和Rolify gem進行身份驗證和權限管理。 但是當我創建一個新的控制器時,我收到了以下消息:

NameError in PanelController#dashboard
uninitialized constant Panel

我的PanelController:

class PanelController < ApplicationController
  load_and_authorize_resource

  def dashboard
  end
end

當我刪除以下行: load_and_authorize_resource ,路由有效。 但我無需身份驗證即可訪問它。 我是否需要PanelModel才能使用它?

我的AbilityModel是這樣的:

class Ability
  include CanCan::Ability

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

    alias_action :create, :read, :update, :destroy, :to => :crud

    if user.has_role? :admin
      can :manage, :all
    elsif user.has_role? :user
      can [:read], User
      can [:update, :edit], User do |account|
        account.email == user.email
      end
    else
      # can :read, :all
      can [:create, :new], User
    end
  end
end

昨天我的代碼很好用,但今天我不知道為什么會收到此錯誤。 也許有人可以幫助我。

我的路線是針對Controller的:

  devise_scope :user do
    authenticated :user do
      # Rails 4 users must specify the 'as' option to give it a unique name
      root :to => "panels#dashboard", :as => :panel
    end

    unauthenticated do
      root 'devise/sessions#new', as: :unauthenticated_root
    end
  end

您可以使用authorize_resource :class => false像這樣在沒有相應模型的情況下使用CanCanCan

class PanelController < ApplicationController
  authorize_resource :class => false

  def dashboard
  end
end

然后以您的能力:

elsif user.has_role? :user
  can :dashboard, :panel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM