Skip to content

Instantly share code, notes, and snippets.

@owahab
Created December 22, 2013 12:54
Show Gist options
  • Save owahab/8082177 to your computer and use it in GitHub Desktop.
Save owahab/8082177 to your computer and use it in GitHub Desktop.
Form errors in a helper
def form_errors object
return '' if object.errors.empty?
if object.errors.count > 1
messages = object.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
sentence = I18n.t("errors.messages.not_saved",
:count => object.errors.count,
:resource => object.class.model_name.human.downcase)
html = <<-HTML
<div data-alert class="alert-box secondary">
<h5>#{sentence}</h5>
<ol class="">#{messages}</ol>
</div>
HTML
else
html = <<-HTML
<div data-alert class="alert-box secondary">
<h5>#{object.errors.full_messages.first}</h5>
</div>
HTML
end
html.html_safe
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment