我正在使用Devise進行身份驗證,並且cancancan進行授權。現在,我希望不允許某些頁面的視圖用戶。所以我添加下面的代碼。但是它會爲#錯誤拋出未定義的局部變量或方法`current_user'。授權查看用戶不使用cancancan訪問rails4應用程序中的某些頁面
我ability.rb
class Ability
include CanCan::Ability
def initialize(dashboard_user)
current_dashboard_user ||= DashboardUser.new
if current_dashboard_user.CD_DASHBOARD_ADMIN?
can :manage, :all
else
can :read, :summary
can :read, :home
end
.........
end
Application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :authenticate_dashboard_user!
protected
rescue_from CanCan::AccessDenied do |exception|
redirect_to main_app.root_url, :alert => exception.message
end
....
end
dashboard_user_controller.rb
class DashboardUsersController < ApplicationController
before_action :set_dashboard_user, only: [:show, :edit, :update, :destroy]
load_and_authorize_resource
....
end
你能告訴我你在哪裏定義你的當前用戶嗎?或者你正在使用哪種模型設計? – Rubyrider 2014-11-22 07:42:51
上午在dashboard_user.rb模型中使用devise @Rubyrider – 2014-11-22 07:45:11
okey,所以問題在某處,您正在使用current_user!你能先找到那個塊嗎? – Rubyrider 2014-11-22 07:45:57