[英]current_user defined in application.rb will not work in create action
[英]How do I get my application.rb file to recognize current_user from active admin?
我正在关注此帖子:
而且我已经把
# if user is not admin redirect to main page
def admin_required
current_user.is_admin? || redirect_to("/")
end
在我的app / controllers / application_controller.rb中
但是我认为我缺少类似的东西
require 'activeadmin'
或其他原因,因为出现以下错误:
未定义的局部变量或方法“ current_user”
我错过了一步吗?
您可以在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.