Skip to content

Instantly share code, notes, and snippets.

@trev
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trev/24cf72e241fbe62db5b1 to your computer and use it in GitHub Desktop.
Save trev/24cf72e241fbe62db5b1 to your computer and use it in GitHub Desktop.

How to customize the humanized :category symbol in the generated error message. Tested with Rails 4.1

class Listing < ActiveRecord::Base    
  validate :premium_category

  private

  def premium_category
    if !user.is_premium && %w(tigers lions rhinos).include?(category))
      errors.add(:category, "not valid for non premium users")
    end
  end
end

config/locales/en.yml

en
 activerecord:
    attributes:
      listing:
        category: "This category is "

This will generate the following error message:

This category is not valid for non premium users

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment