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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With