2

出力がEUR、USD、.....ユーロ、米ドルの場合に選択するための(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_NAMESt.name

Currency.get_active.collect{ |t| [Currency.LONG_NAMES[t.name] || t.name, t.id] }
于 2013-01-07T15:37:50.697 に答える