Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise not displaying error messge during an authentication failure?

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 didn't change any stuffs. The default devise installation itself is not displaying flash errors with invalid authentication attempt. I just installed devise as a gem a tryed to uise it. Din't even change a word of the code that is generated.
May be I thought if is it due to some browser comparability issues.
But I get other mannually introduced flash messsages else working.

Any suggestions as to what may be broken.

I'm using rails 3.0.1
* Update *

I'm getting failure messages for user registration(sign up) but not for failures messages of signing in. some googleing on this topic revealed that for sign up it expects:-
<%= devise_error_messages! %> But for sign in it expects some other alert message tag to be referenced, but didn't get the exact info of what is that alert tag which i must use and wher to use ???

Please provide some suggestions !!!
Thanks in advance.

like image 528
Hemanth Avatar asked Nov 06 '10 05:11

Hemanth


2 Answers

Atlast after some good amount of searching/browsing I found the answer,
you have to add the following piece of code in our application.html.erb file

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

After adding this I was able to see the sign_in failures alert messages :).

like image 177
Hemanth Avatar answered Sep 18 '22 07:09

Hemanth


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
    <div id="error_explanation">
      <h2>#{sentence}</h2>
      <ul>#{messages}</ul>
    </div>
    HTML

    html.html_safe
  end

end
like image 20
typeoneerror Avatar answered Sep 20 '22 07:09

typeoneerror