Skip to content

Instantly share code, notes, and snippets.

@scarfacedeb
Created May 13, 2015 11:13
Show Gist options
  • Save scarfacedeb/cda3cda4db1f16d45e1f to your computer and use it in GitHub Desktop.
Save scarfacedeb/cda3cda4db1f16d45e1f to your computer and use it in GitHub Desktop.
Helper method to check validation errors
# required: Rails 5 or active_model-errors_details gem
# see: https://cowbell-labs.com/2015-01-22-active-model-errors-details.html
module CheckValidationDetails
# Check if the given error exists on given attribute
#
# @param attribute [Symbol] model attribute
# @param error [Symbol] machine-friendly error name
# @return [Boolean] whether or not any given error exists
def error?(attribute, error)
errors.details.key?(attribute) && errors.details[attribute].any? { |err| err[:error] == error }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment