Search code examples
ruby-on-railsruby-on-rails-4adminactiveadmin

undefined method `is_admin?' for nil:NilClass :(


I want to restrict the access to my pannel admin (gem active_admin) for admin only.

That's my code

class ApplicationController < ActionController::Base
    def authenticate_admin!
        unless current_user.is_admin?
            flash[:error] = "Access denied"
            redirect_to root_path
        end
    end

and the problem is : undefined method `is_admin?' for nil:NilClass

there is a boolean admin (0 false, true 1) in my DB

I've to define my is_admin?, but i try and he is never found. So where do i have to do that ?

Thx for your help


Solution

  • The issue is in your error message. nil doesn't have the method 'is_admin?'. This means that your current user variable isn't being set. You need to redirect users who are not logged-in to a screen where they can do so. Then direct them either through this authenticate_admin! function either first to redirect all user who are admin to the /admin path or simply push all users to your home page allowing them to click an admin link.