Am using Devise for authentication and cancancan for authorization.Now i want to not allow the view user for some pages. So i add following codes. But it throws undefined local variable or method `current_user' for # error.
My 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
Okey, just do this trick for now. Somehow the current_user helper method is being called. So the quickest solution would be if you can do the following.
In your
application_controller.rb
file put this block:I hope this will help.