![](/img/trans.png)
[英]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.