0

我有 2 个帮手:

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

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

帮手1:

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

助手 2:

["ZWN", "ZWR"]

我需要在选择字段的值属性中指定第二个助手。我试过这个:

<%= 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?

谢谢!

4

1 回答 1

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" %>
于 2013-06-29T12:35:43.243 回答