in my user edit page, there is a line as follows:
<%= devise_error_messages! %>
The problem is this does not output errors the standard way that the rest of the app does:
<% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
My question is, how do I get the devise error message to work like the others that use the flash.each?
Thanks.
If you are looking to piggyback off of devise_error_messages then you can so by adding to resource.errors
If you were to over ride the registration controller, it might look like
I just created an
app/helpers/devise_helper.rb
like John but overrode the method like that :With this I don't have to modify anything else. Is it a bad idea ? I'm new to rails, don't hesitate to correct me. Thanks.
If you want to be able to display more than one flash of a given type (:alert, :notice, etc...) and not waste your time trying to modify a gem behavior, this is the solution I used with Devise. I'm pretty sure it could be used with any gem that uses flash messages.
First thing to do, in your application_controller.rb, add this:
Second thing to do, displaying your flash messages with this in application.html.erb (or wherever you want):
Third thing to do, whenever you want to add a flash message in any controller, do this:
Very easy way to display error message for each field
put for each field with field name in square bracket below every line where u want to display inline error message.
To show your devise error from your controller with only the first error to showing up.
I know it's been a while since this question was posted, but I just wanted to comment on what I've found. The two people who've already answered have been a tremendous help to me and I just wanted to contribute.
You'll see throughout Devise that there are calls using
render_with_scope
. I believe this is a method defined by devise and basically applies the current scope to the next view rendered.Why is this relevant? Devise contains your errors within
resource.errors
(not@resource.errors
). Devise works fine if you want to use it out of the box, so to speak.Problems with these errors arise if you start changing your user management behavior. By adding a
redirect_to
orrender
(instead ofrender_with_scope
) where Devise previously didn't have one, you're basically tossing out the error messages. This makes Devise unfriendly to modification, in my opinion.My solution is this
and
The latter code block takes Devise's error messages as an array and appends it to
flash[:notice]
(as an array). Each message will be printed out one line at a time. If I have the time, I think I'm going to change how Devise handles error messages to do this throughout my app, as it seems much cleaner to have one error message system instead of two.