Devise not displaying error messge during an authentication failure?

前端 未结 4 1722
名媛妹妹
名媛妹妹 2021-01-03 22:35

I was expecting a flash notice when authentication failures occurs in devise. But get nothing during a authentication failure, just the page refreshes and remains still. I

4条回答
  •  甜味超标
    2021-01-03 23:28

    Admittedly, a bit hacky, but I'm using this helper (app/helpers/devise_helper.rb) to grab flashes and use those if set then default to resource.errors. This is just based on the helper that's in the devise lib.

    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
        

    #{sentence}

      #{messages}
    HTML html.html_safe end end

提交回复
热议问题