Skip to content

Instantly share code, notes, and snippets.

@scharfie
Last active December 15, 2015 07:10
Show Gist options
  • Save scharfie/5221827 to your computer and use it in GitHub Desktop.
Save scharfie/5221827 to your computer and use it in GitHub Desktop.
i18n lambda + using view helpers in error messages
# config/locales/en.rb
{
:en => {
:activerecord => {
:errors => {
:models => {
:bid => {
:price_too_low => lambda { |errors,attributes|
helpers = ActionController::Base.helpers
currency = attributes[:price] || 0
formatted_currency = helpers.number_to_currency(currency)
"Your bid must be greater than #{formatted_currency}"
}
}
}
}
}
}
}
bid = Bid.new
bid.errors.add(:base, :price_too_low, :price => 123.45)
bid.errors.full_messages # => Your bid must be greater than $123.45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment