0

我有2名助手:指定值options_for_select輔助

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在選擇字段文本?

謝謝!

回答

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" %> 
+0

謝謝,你的修復工作正常。謝謝! – hyperrjas

+0

@hyperrjas太棒了! :) – lurker