Skip to content

Instantly share code, notes, and snippets.

@CyberStrike
Last active August 29, 2015 14:05
Show Gist options
  • Save CyberStrike/ad181d48025f5f4b0f41 to your computer and use it in GitHub Desktop.
Save CyberStrike/ad181d48025f5f4b0f41 to your computer and use it in GitHub Desktop.
Form Field Errors Rails 4
= error_message_for(:resource, :field_name, :prepend_text => '')
module ApplicationHelper
# Error messages for the specific form field
def error_message_for(resource, field, options = {:prepend_text => false})
error_message = resource.errors[field]
# If the error messages are not empty put the error messages
# and the prepended text unless it's empty then put the field name.
unless error_message.empty?
"#{options[:prepend_text] || field.to_s.capitalize} #{error_message.to_s}".delete! '[]""'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment