Overriding Default Error Messages, New Styley!

Aug 21st — 3:50 PM

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" 
    }
  }
}

Filed under: rails
no comments

Lucky Sneaks is designed and managed by Lucky Sneaks
All content © Lucky Sneaks 2008
Lucky Sneaks is Lucky Sneaks