我使用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
关于ruby-on-rails - cancancan load_and_authorize_resource NameError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32167219/