Devise: binding 'resource' (aka general error reporting)

The much maligned but otherwise innocent "devise_error_messages!" method in the DeviseHelper module starts out with:

def devise_error_messages!     return "" if resource.errors.empty?     ... end

My question: where is 'resource' set up? I'd like to replicate its functionality, but my naive approach (creating a FooHelper module in app/helpers) fails because 'resource' isn't bound.

Another way to ask the question: rather than creating specific error handlers for User, Topic, Profile, etc, I'd like to write a single error handling helper that can be used for any model.

Right now each of my _form.html.erb files start with something like:

<% if @user.errors.any? %> <div id="error_explanation">   <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>   <ul>     <% @user.errors.full_messages.each do |msg| %>     <li><%= msg %></li>     <% end %>   </ul> </div> <% end %>

... but it's clearly unDRY to replicate that for @user, @topic, @profile. How can I write a helper that will generalize to the current resource?

- ff

P.S.: Anyone looking to eliminate the "DEPRECATION WARNING: f.error_messages was removed from Rails" warning might be interested in the answer to this thread. Now it will turn up in a search...

I figured out the answer to my own question, and I'm embarrassed by how trivial it is: create a helper function that takes @user, @topic, @profile etc as an argument. For example:

# app/helpers/my_helper.rb module MyHelper   def my_error_message(obj)     return unless obj.errors.any?     summary = "#{pluralize(obj.errors.count, "error")} prevented this #{obj.class} from being saved:"     msgs = obj.errors.full_messages.map { |msg| content_tag(:li, msg) }.join     "<div id=\"error_explanation\"><h3>#{summary}</h3><ul>#{msgs}</ul></div>".html_safe   end end

Now you can sprinkle <%= my_error_message(@user) %> in the top of user/_form.html.erb, etc. Works like a champ.

- ff

Much obliged for this answer!