2

如果输出是欧元,美元,如何更改(在 Rails 中)选项,我想要欧元,美元。

<%= select_tag('user[currency_id]', options_for_select(Currency.get_active.collect{|t| [t.name, t.id]}, @user.try(:currency_id) ), {:class => "bigselect"})  %>

其他人有想法吗?

在此先感谢您的帮助。

4

1 回答 1

5

你应该用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] }
于 2013-01-07T15:37:50.697 回答