Created
February 14, 2012 19:19
-
-
Save geronimod/1829400 to your computer and use it in GitHub Desktop.
customize error messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def error_messages_for(objects, options = {}) | |
objects = Array(objects) | |
count = objects.inject(0) {|sum, object| sum + object.errors.count } | |
unless count.zero? | |
html = {} | |
[:id, :class].each do |key| | |
if options.include?(key) | |
value = options[key] | |
html[key] = value unless value.blank? | |
else | |
html[key] = 'errorExplanation' | |
end | |
end | |
if count > 1 then | |
header_message = _("%{ct_errores} errores han impedido procesar esta petición") % {:ct_errores => count} | |
else | |
header_message = _("Un error ha impedido procesar esta petición") % {:ct_errores => count} | |
end | |
error_messages = objects.map {|object| object.errors.full_messages.map { |msg| | |
# si el mensaje empieza por ^ borramos el nombre del campo | |
msg = msg.sub(/.*?\^/, "") | |
content_tag(:li, _(msg)) } | |
} | |
content_tag(:div, | |
content_tag(options[:header_tag] || :h2, header_message) << | |
content_tag(:p, options[:subheader_message] || _('Hubo errores en los siguientes campos') + ':') << | |
content_tag(:ul, error_messages), | |
html | |
) | |
else | |
'' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment