Is it possible to create a select tag from the models validation without having problems with I18N?
For example if i had a model like this:
Model:
class Coffee < ActiveRecord::Base
SIZES = [ "small", "medium", "big" ]
validates :size, :inclusion => { :in => SIZES,
:message => "%{value} is not a valid size" }
end
Form:
<%= f.label :size %><br />
<%= select(:coffee, :size, Coffee::SIZES.collect {|d| [d, d]}) %>
How can I make this language independent?
If you’re trying to make the validation message i18n independent, you don’t actually need to mention which size is invalid, just that it is. You’re passing an HTML select form, if they chose another size it’s more likely they’re messing with something so an exact error message is unnecessary.
For the select text itself, you could just pass it to the i18n system and handle it in that. By building the array with
Coffee::SIZE.collect {|d| [t(".#{d}"), d]}you could addsmall,medium,bigto your i18n file for that view to get localized values based off your validation options.