3

我使用 CanCanCan、Devise 和 Rolify gem 来进行身份验证和权限管理。但是当我创建一个新控制器时,我收到了这条消息:

NameError in PanelController#dashboard
uninitialized constant Panel

我的面板控制器:

class PanelController < ApplicationController
  load_and_authorize_resource

  def dashboard
  end
end

当我删除此行时:load_and_authorize_resource 路线有效。但我无需身份验证即可访问它。我需要 PanelModel 才能使用它吗?

我的能力模型是这样的:

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

昨天我的代码运行良好,但今天我不知道为什么会出现此错误。也许任何人都可以帮助我。

我的路线是控制器的:

  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
4

1 回答 1

7

您可以使用 CanCanCan 没有相应的模型使用authorize_resource :class => false这样

class PanelController < ApplicationController
  authorize_resource :class => false

  def dashboard
  end
end

然后在你的能力范围内:

elsif user.has_role? :user
  can :dashboard, :panel
于 2015-08-23T14:06:02.790 回答