Devise Not Displaying Error Messge During an Authentication Failure

Devise not displaying error messge during an authentication failure?

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 :).

Why is devise not displaying authentication errors on sign in page?

A login with blank/wrong fields does not trigger (your) validations on the model, and therefore won't show your validation errors !

if you debug with byebug (in the first line of your view for example), you'll notice

resource.errors.count # => 0
flash # => ....@flashes={"alert"=>"Invalid email or password."}

Devise populates the "alert flash" with specific sign in error messages unique to this context of sign-in.

Why do you not see all model validation error messages ? Because that wouldn't make sense : suppose your model has a mandatory :gender attribute with validates_presence_of :gender. If normal model errors were added, then you would also see "gender cannot be blank" in the list of errors when your user tries to sign in with a wrong login :oops:.

devise_error_messages! is a specific devise method meant to show those specific errors. You can think of it as a partial validation on the fields that are used for sign in (and that are defined in your devise config file)

WORKAROUND :

If you really want to show all your error messages, you could just explicitely run the validations :

at the beginning of devise_error_messages!

resource.validate # It will generate errors and populate `resource.errors`

I believe it shouldn't mess up with other actions that already work well (register, etc.)

Devise errors not working?

Devise error messages are added to flash and should show as long as your application.html.erb file includes them, you can read the details in Ryan Bates excellent railscast

http://railscasts.com/episodes/209-devise-revised?view=asciicast

Also look at this post
Devise not displaying error messge during an authentication failure?

Devise login with username, signin failure not showing error message

I think you are not displaying the flash messages.
So add the below in your application.html.erb above <%= yield %>

<% flash.each do |key, value| %>
<%= content_tag :div, value, class: "flash #{key}" %>
<% end %>

Rails 3, Devise not displaying error message on authentication failure

Could it be that the flash message is getting eaten by the redirect? The flash is only good for the next request, so maybe the redirect is getting the flash message, but it's gone on the next request (root_url). You might have to do an internal redirect or pass the flash vars through to the next request.

Can I ask why you're doing an external redirect on failure instead of just showing the login form again with the error messages?

Devise log_in errors not showing

The question was tagged with 'devise' so I assume you are using it. Devise errors are not flash messages and not implementing this mechanism. Take a look into this partial devise_error_messages!. You could include it inside your Devise form.

Update

I am updating my answer following the question updates. I've created a new app with three Devise models as described and used the code snippet from the question to generate the flashes. I've followed the tutorial from the question description and was able to reproduce your issue. It appears that the flash.clear in the concern clears the flash when user redirects back to the new session view.

You could add the following:

flash.clear unless params['action'] == 'new'

as a quick hack to prevent the flash clear on redirects back to new session page.



Related Topics



Leave a reply



Submit