Module: ErrorMessagesHelper

Defined in:
lib/generators/zoo/layout/templates/error_messages_helper.rb

Defined Under Namespace

Modules: FormBuilderAdditions

Instance Method Summary collapse

Instance Method Details

#error_messages_for(*objects) ⇒ Object

Render error messages for the given objects. The :message and :header_message options are allowed.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/generators/zoo/layout/templates/error_messages_helper.rb', line 3

def error_messages_for(*objects)
  options = objects.extract_options!
  options[:header_message] ||= I18n.t(:"activerecord.errors.header", default: "Invalid Fields")
  options[:message] ||= I18n.t(:"activerecord.errors.message", default: "Correct the following errors and try again.")
  messages = objects.compact.map { |o| o.errors.full_messages }.flatten
  unless messages.empty?
    (:div, class: "error_messages") do
      list_items = messages.map { |msg| (:li, msg.html_safe) }
      (:h2, options[:header_message].html_safe) + (:p, options[:message].html_safe) + (:ul, list_items.join.html_safe)
    end
  end
end