-
-
Save foca/a6ffe32e51c21ccd280e9dbe2307f88a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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