指定值options_for_select帮助程序

时间:2013-06-29 12:10:18

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我有2名助手:

all_currencies(Money::Currency.table)
all_currencies_values(Money::Currency.table)

每个助手都是一个具有不同值的数组:

帮手1:

["ZWN, Zimbabwean Dollar, $", "ZWR, Zimbabwean Dollar, $"]

帮手2:

["ZWN", "ZWR"]

我需要在select字段的value属性中指定第二个帮助器。我试过这个:

<%= select_tag "currency", all_currencies_values(Money::Currency.table)), options_for_select(all_currencies(Money::Currency.table), :prompt => "Select currency" %>

但是我在选择字段的文本和值中得到相同的结果:

如何将帮助器2 all_currencies_values(Money::Currency.table)添加到选择字段值中的选择字段值和助手1?

谢谢!

1 个答案:

答案 0 :(得分:2)

options_for_select正在寻找文本/值配对。所以你可以这样做:

<%=
   money_text = all_currencies(Money::Currency.table)
   money_values = all_currencies_values(Money::Currency.table)
   select_tag "currency", options_for_select(money_text.zip(money_values)), :prompt => "Select currency"
%>

或者,创建一个帮助器(例如,currency_selects),它已经为您提供了[[text1, val1], [text2, val2], ...]的文本/值对,并在一行中执行:

<%= select_tag "currency", options_for_select(currency_selects(Money::Currency.table), :prompt => "Select currency" %>