Last active
December 15, 2015 07:10
-
-
Save scharfie/5221827 to your computer and use it in GitHub Desktop.
i18n lambda + using view helpers in error messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}" | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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