Fearless Fool
2010-Nov-12 03:29 UTC
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...
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2010-Nov-12 20:48 UTC
Re: Devise: binding ''resource'' (aka general error reporting)
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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
pgpkeys
2010-Nov-16 22:33 UTC
Re: Devise: binding ''resource'' (aka general error reporting)
Much obliged for this answer! On Nov 12, 3:48 pm, Fearless Fool <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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_sa fe > end > end > > Now you can sprinkle <%= my_error_message(@user) %> in the top of > user/_form.html.erb, etc. Works like a champ. > > - ff > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.