Skip to content

Instantly share code, notes, and snippets.

@waldofe
Last active August 29, 2015 14:04
Show Gist options
  • Save waldofe/83e7d18569c82678b838 to your computer and use it in GitHub Desktop.
Save waldofe/83e7d18569c82678b838 to your computer and use it in GitHub Desktop.
Simple alert error message helper
module ApplicationHelper
# Renders the first field error of an object attribute on a red alert.
#
# @param klass [Class Object] - This object needs to
# include ActiveModel::Validations (or be a normal ActiveModel)
# @param field_sym [Symbol] - Attribute or field name of the object
def show_error_message_for(klass, attribute)
if klass.errors[attribute].any?
%(
<div class="alert alert-danger">
#{klass.errors.full_messages_for(attribute).first}
</div>
).html_safe
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment