[英]Redirect to Index if user is not logged in
我的application_controller.rb中有這個:
private
def require_user_signed_in
unless user_signed_in?
# If the user came from a page, we can send them back. Otherwise, send
# them to the root path.
if request.env['HTTP_REFERER']
fallback_redirect = :back
elsif defined?(root_path)
fallback_redirect = root_path
else
fallback_redirect = "/"
end
redirect_to fallback_redirect, flash: {error: "You must be signed in to view this page."}
end
end
我在正文開始后立即將其添加到application.html.erb(頁面布局)中:
<% require_user_signed_in %>
我的問題是為什么它不起作用? 我收到未定義的局部變量或方法錯誤。 我沒有正確調用該方法嗎? 我是否必須在某處定義一些內容才能在布局erb中使用?
謝謝。
將此方法添加到application_helper.rb
或使其成為輔助方法,如果要將其保留在application_controller.rb
,請刪除private
並添加以下行
helper_method :require_user_signed_in
希望有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.