【发布时间】:2015-08-23 13:35:13
【问题描述】:
我使用 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 才能使用它吗?
我的 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
昨天我的代码运行良好,但今天我不知道为什么会出现此错误。 也许任何人都可以帮助我。
我的路线是给控制器的:
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
【问题讨论】:
-
是的,load_and_authorize_resource 正在寻找 Panel 模型。
-
但是为什么呢?我如何在不尝试查找 PanelModel 但通过身份验证的情况下使用它?
-
在没有 Panel 模型的情况下,您将如何在 Ability 类中检查仪表板操作的权限?
-
我可以用 routes.rb 查一下吗?
-
可以,但为什么还需要使用 CanCanCan 进行另一次检查?
标签: ruby-on-rails ruby-on-rails-4 cancan cancancan