Overriding Default Error Messages, New Styley!
I’m posting this for anyone who’s googling how to replace their old school error message overrides with the new Rails’ I18n-compatible version.
Old school [please note this is the old, deprecated way just shown here for google to hook into for people searching on various lines of the old code]:
module ActiveRecord
class Errors
@@default_error_messages.merge!({
:invalid => "doesn't appear to be valid",
:too_long => "can't be more than %d characters",
:too_short => "can't be less than %d characters",
:wrong_length => "must be exactly %d characters",
:taken => "is already taken",
})
end
end
New school:
I18n.backend.store_translations "en-US", {
:active_record => {
:error_messages => {
:invalid => "doesn't appear to be valid",
:too_long => "can't be more than %d characters",
:too_short => "can't be less than %d characters",
:wrong_length => "must be exactly %d characters",
:taken => "is already taken"
}
}
}