简体   繁体   中英

How do I get my application.rb file to recognize current_user from active admin?

I am I am following this post:

Active Admin - Same model for users and admins

and I have put

# if user is not admin redirect to main page
def admin_required
  current_user.is_admin? || redirect_to("/")
end

In my app/controllers/application_controller.rb

However I think I am missing something like

require 'activeadmin'

or something because I am getting the following error:

undefined local variable or method `current_user'

Have I missed a step or something?

You can define current_user method in app/controller/application_controller.rb and then make it available as a helper_method:

def current_user
  return unless session[:user_id]
  @current_user ||= User.find_by_id(session[:user_id])
end
# Make current_user available in templates as a helper
helper_method :current_user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM