8

Using Rails 3.07 and Devise 1.1.5

Everything is working fine and as expected with one exception. When a user tries to login with a bogus password, for instance, devise denies the login attempt, which is correct, but fails to provide an error message.

I have several error display methods set up in app/helpers/devise_helper.rb and I am using one called devise_sign_in_error_messages! for the login view. Therefore I am able to verify that the following line from that function is returning a blank string for errors in this case: return "" if resource.errors.empty?

If I give a correct username and password, the system logs me in just fine, so all of the devise logic seems fine, it's just this lack of an error message that's a mystery.

What do I need to change to help devise pass me an error message on failed login?

EDIT:

The answer is that: a) devise is sticking the answer in flash b) even though it's in flash, it's not using the key you might expect

Using this bit of code, I can see the message:

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

Which I came across on a different post as an answer to a different question: Another stack overflow post

I had tried to output the flash earlier but did not see the message because I followed a bit of code from a different stack overflow post that appears to be insufficient. Namely, I had tried:

<%= flash[:message] if flash[:message]
flash[:warning] if flash[:warning]
flash[:error] if flash[:error] %>

Devise is not using these keys for the login error message so this code will get you nothing.

I find devise's handling of this to be inconsistent. Specifically, if I choose devise's forgot password option and enter a bogus email address for instance, the error gets passed back in the resource.errors array, but here with the bad login it gets passed in flash.

Community
  • 1
  • 1
Masterchief
  • 81
  • 1
  • 3
  • 4
    you should post your answer as an answer(which you can accept later) and not as an edit :) – oers Sep 01 '11 at 11:20
  • 3
    Please post your answer as an answer, not as an edit, otherwise it will stay on stackoverflow as unaswered – Calavera Feb 26 '12 at 18:16

1 Answers1

15

As you have discovered Devise does not use flash[:message], flash[:warning], and flash[:error].

Devise uses flash[:notice] and flash[:alert].

It's not an easy find in the documentation but is just under the third point in Configuring controllers.

Hope this clears things up.

Phil Dudley
  • 326
  • 3
  • 4