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 [updated to latest edge Rails]:
I18n.backend.store_translations "en-US", {
:activerecord => {
:errors => {
:messages => {
:invalid => "doesn't appear to be valid",
:too_long => "can't be more than {{count}} characters",
:too_short => "can't be less than {{count}} characters",
:wrong_length => "must be exactly {{count}} characters",
:taken => "is already taken"
}
}
}
}
2 comments
Leave a Comment