簡體   English   中英

如何獲取我的application.rb文件以從活動管理員中識別current_user?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM