Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leonardofaria/bf88d8fb1e5cf1fd2a0b to your computer and use it in GitHub Desktop.
Save leonardofaria/bf88d8fb1e5cf1fd2a0b to your computer and use it in GitHub Desktop.
module ErrorMessagesHelper
# Render error messages for the given objects. The :message and :header_message options are allowed.
def error_messages_for(*objects)
options = objects.extract_options!
options[:header_message] ||= t(:"errors.template.header", model: t(:"activerecord.models.#{objects.compact.first.class.name.downcase}"), count: objects.compact.first.errors.messages.size)
options[:message] ||= t(:"errors.template.body")
messages = objects.compact.map { |o| o.errors.full_messages }.flatten
unless messages.empty?
content_tag(:div, id: "error_explanation") do
list_items = messages.map { |msg| content_tag(:li, msg) }
content_tag(:h2, options[:header_message]) + content_tag(:p, options[:message]) + content_tag(:ul, list_items.join.html_safe)
end
end
end
module FormBuilderAdditions
def error_messages(options = {})
@template.error_messages_for(@object, options)
end
end
end
ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderAdditions)
@noctivityinc
Copy link

This is great but where do we put it? I added it to app/helpers in a Rails 5 app and it doesnt seem to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment