Francis's Octopress Blog

A blogging framework for hackers.

Rails - Devise - Handling - Devise_error_messages

rails – Devise – Handling – devise_error_messages

in my user edit page, there is a line as follows:

<%= devise_error_messages! %> 

The problem is this does not output errors the stand way that the rest of the app does:

<% flash.each do |key, value| %>     <divpun" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: rgb(0, 0, 0); "><%= key %>"><%= value %></div> <% end %> 

My question, is how to I get the devise error message to work like the others that use the flash.each?

Thanks

I’m trying to figure this out myself. I just found this issue logged on Githubhttps://github.com/plataformatec/devise/issues/issue/504/#comment_574788

Jose is saying that devise_error_messsages! method is just a stub (though it contains implementation) and that we’re supposed to override/replace it. It would have been nice if this was pointed out somewhere in the wiki, which is why i guess there are a few people like us that have been guessing.

So I’m going to try reopening the module and redefine the method, effectively overriding the default implementation. I’ll let you know how it goes.

Update

Yep, that works. I created app/helpers/deivse_helper.rb and overrode it like so:

module DeviseHelper   def devise_error_messages!     'KABOOM!'   end end 

So knowing this, I can modify the method to display error messages the way I want it to.

To help you solve your original problem: Here’s the original devise_helper.rb on Github. Take a look at how the error messages are being traversed, specifically Line 5.

messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join 

That should help you get started. :)

Another update

The resource object is actually the model that is being used by devise (go figure).

resource.class         #=> User resource.errors.class  #=> ActiveModel::Error 

It also appears to be defined in a higher scope (probably coming from the controller), so it can be accessed in a variety of places.

Anywhere in your Helper

module DeviseHelper   def devise_error_messages1!     resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join   end   def devise_error_messages2!     resource.errors.full_messages.map { |msg| content_tag(:p, msg) }.join   end end 

Your View

<div><%= resource.errors.inspect %></div>

More info look at http://stackoverflow.com/questions/4101641/rails-devise-handling-devise-error-messages