繁体   English   中英

Rails 3,Devise在身份验证失败时不显示错误消息

[英]Rails 3, Devise not displaying error message on authentication failure

我正在使用Rails 3和Devise。

我在{rails_root} /lib/custom_failure.rb中有此文件,并在我的Devise初始化程序中有所需的加载代码。

class CustomFailure < Devise::FailureApp

  def redirect_url
    root_path
  end

  def respond
    if http_auth?
      http_auth
    else
      redirect_to root_url
    end
  end
end

验证失败后,将加载文件,并将用户重定向到root_url。 问题是验证失败时不会显示错误消息,但是所有其他设计消息运行正常(成功登录等)。

我的应用程序布局中有这个

  <% flash.each do |name, msg| %>
    <%= content_tag :div, msg, :id => "flash_#{name}" %>
  <% end -%>

编辑:最终的工作代码。 原来我最大的错误是没有重新启动服务器。 CustomFailure是一个lib,仅加载一次。

  def respond
    if http_auth?
      http_auth
    else
      flash[:notice] = "Error message goes here"
      redirect_to root_url
    end
  end

可能是Flash消息被重定向吞噬了吗? Flash仅对下一个请求root_url ,因此重定向可能会收到Flash消息,但它在下一个请求( root_url )中消失了。 您可能必须执行内部重定向,或将Flash变量传递给下一个请求。

请问为什么您要在发生故障时进行外部重定向,而不是仅显示带有错误消息的登录表单?

您也可以在响应动作中添加flash[:alert]

class CustomFailure < Devise::FailureApp
    def redirect_url
        root_url
    end
    def respond
        if http_auth?
            http_auth
        else
            flash[:alert] = i18n_message unless flash[:notice]
            redirect_to root_url
        end
    end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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