代码之家  ›  专栏  ›  技术社区  ›  Hemanth

设计在身份验证失败期间不显示错误消息?

  •  14
  • Hemanth  · 技术社区  · 14 年前

    我没有换任何东西。默认的设备安装本身没有显示带有无效身份验证尝试的flash错误。我刚刚安装了一个宝石的设计,试图消除它。甚至连生成的代码的一个字都不改变。
    可能是因为浏览器的可比性问题。

    任何可能被破坏的建议。

    我使用的是Rails3.0.1

    我收到的失败消息用于用户注册(注册),但不用于登录失败消息。
    <%=设计错误消息!%> 但是对于登录,它希望引用其他一些警报消息标记,但是没有得到必须使用的警报标记和使用的确切信息???

    请提供一些建议!!!

    4 回复  |  直到 13 年前
        1
  •  40
  •   Hemanth    14 年前

    至少经过大量的搜索/浏览我找到了答案,
    您必须在application.html.erb文件中添加以下代码

      <%- flash.each do |name, msg| -%>
        <%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
      <%- end -%>  
    

        2
  •  2
  •   typeoneerror    13 年前

    不可否认,有点老套,但我使用这个助手(app/helpers/devieve_helper.rb)来抓取闪光灯,并使用那些如果设置为默认值 resource.errors

    module DeviseHelper
    
      def devise_error_messages!
        flash_alerts = []
        error_key = 'errors.messages.not_saved'
    
        if !flash.empty?
          flash_alerts.push(flash[:error]) if flash[:error]
          flash_alerts.push(flash[:alert]) if flash[:alert]
          flash_alerts.push(flash[:notice]) if flash[:notice]
          error_key = 'devise.failure.invalid'
        end
    
        return "" if resource.errors.empty? && flash_alerts.empty?
        errors = resource.errors.empty? ? flash_alerts : resource.errors.full_messages
    
        messages = errors.map { |msg| content_tag(:li, msg) }.join
        sentence = I18n.t(error_key, :count    => errors.count,
                                     :resource => resource.class.model_name.human.downcase)
    
        html = <<-HTML
        <div id="error_explanation">
          <h2>#{sentence}</h2>
          <ul>#{messages}</ul>
        </div>
        HTML
    
        html.html_safe
      end
    
    end
    
        3
  •  0
  •   GuilPejon    7 年前

    我知道这已经很旧了,但我最终来到这里是因为我在rails 5.1中遇到了同样的问题,而接受的答案不起作用,所以我做了以下这些。重写Devise::SessionController后,向其中添加以下代码:

    after_action :unauthenticated
    
    protected
    
    def unauthenticated
      flash[:alert] = t("devise.failure.#{request.env['warden'].message}") unless request.env['warden'].message.blank?
    end
    

    另外,在同一个控制器上,复制并粘贴Devise版本中create方法的代码,并删除 ! warden.authenticate! . 因为你删除了 ! ,现在必须检查资源是否为nil,如果为nil,则重定向。在我的例子中,create方法的结果是这样的:

    def create
      self.resource = warden.authenticate(auth_options)
      redirect_to root_path and return if resource.nil?
      set_flash_message!(:notice, :signed_in)
      sign_in(resource_name, resource)
      yield resource if block_given?
      respond_with resource, location: after_sign_in_path_for(resource)
    end
    

    最后,您只需在视图上打印flash消息。我使用的是materialize,因此我创建了一个partial并向它添加了以下代码(您应该根据自己的需要进行自定义):

    <% flash.each do |type, message| %>
      <% if type == "notice" %>
        <script id="toast">
          $(function() {
            Materialize.toast('<%= message %>', 4000);
          });
        </script>
      <% elsif type == "success" %>
        <script id="toast">
          $(function() {
            Materialize.toast('<%= message %>', 4000, 'green');
          });
        </script>
      <% elsif type == "alert" %>
        <script id="toast">
          $(function() {
            Materialize.toast('<%= message %>', 4000, 'orange');
          });
        </script>
      <% end %>
    <% end %>
    
        4
  •  0
  •   Ralf    6 年前

    在窗体内部,错误可以显示为:

    <% resource.errors.full_messages.each do |msg| %>
        <%= msg %>
    <% end %>