简体   繁体   English

如何获取我的application.rb文件以从活动管理员中识别current_user?

[英]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 在我的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' 未定义的局部变量或方法“ 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: 您可以在app/controller/application_controller.rb定义current_user方法,然后将其用作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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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