繁体   English   中英

如果用户未登录,则重定向到索引

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

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