Skip to content

Instantly share code, notes, and snippets.

@foca

foca/errors.rb Secret

Last active May 20, 2016 11:53
Show Gist options
  • Save foca/a6ffe32e51c21ccd280e9dbe2307f88a to your computer and use it in GitHub Desktop.
Save foca/a6ffe32e51c21ccd280e9dbe2307f88a to your computer and use it in GitHub Desktop.
DEFAULT_MAPPINGS = {
not_present: "can't be empty".freeze,
# …
}
ERROR_MARKUP = '<p class="error-message">%s.</p>'.freeze
def error_message(form, field, **overrides)
mappings = DEFAULT_MAPPINGS.merge(overrides)
if form.errors[field].any?
messages = form.errors[field].map { |code| mappings.fetch(code) }
ERROR_MARKUP % messages.join(", ")
end
end
# And then
# <%= error_message(form, :password, too_short: "must be at least 8 characters long") %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment