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? %>
<%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:
-
<% @user.errors.full_messages.each do |msg| %>
- <%= msg %> <% 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…