如果输出是欧元,美元,如何更改(在 Rails 中)选项,我想要欧元,美元。
<%= select_tag('user[currency_id]', options_for_select(Currency.get_active.collect{|t| [t.name, t.id]}, @user.try(:currency_id) ), {:class => "bigselect"}) %>
其他人有想法吗?
在此先感谢您的帮助。
如果输出是欧元,美元,如何更改(在 Rails 中)选项,我想要欧元,美元。
<%= select_tag('user[currency_id]', options_for_select(Currency.get_active.collect{|t| [t.name, t.id]}, @user.try(:currency_id) ), {:class => "bigselect"}) %>
其他人有想法吗?
在此先感谢您的帮助。
你应该用I18n翻译它们:
Currency.get_active.map{ |t| [I18n.t("currencies.names.long.#{t.name}"), t.id] }
在您的locale.yml中(例如en.yml):
# en.yml
currencies:
names:
long:
usd: "US Dollars"
eur: "Euros"
#...
short:
usd: "$US"
eur: "€"
或者没有翻译系统的替代方案:
class Currency < ActiveRecord::Base
LONG_NAMES = {
'EUR' => 'Euros',
'USD' => 'US Dollars',
# ...
}
# ...
end
并像这样使用它:
Currency.get_active.collect{ |t| [Currency.LONG_NAMES[t.name], t.id] }
如果t.name
返回不在LONG_NAMES
常量中的条目,则显示t.name
属性:
Currency.get_active.collect{ |t| [Currency.LONG_NAMES[t.name] || t.name, t.id] }