hyperrjas
hyperrjas

Reputation: 10744

specify value options_for_select helper

I have 2 helpers:

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

Each helper is an array with differents values:

Helper 1:

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

Helper 2:

["ZWN", "ZWR"]

I need specify the second helper in value attribute of select field. I have tried with this:

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

but I get the same result in both, text and value of select field:

How can I add the helper 2 all_currencies_values(Money::Currency.table) to the select field value and helper 1 in select field text?

Thanks!

Upvotes: 0

Views: 251

Answers (1)

lurker
lurker

Reputation: 58224

options_for_select is looking for the text/value pairings. So you could do this:

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

Or, create a helper (e.g., currency_selects) that already gives you the text/value pairings as [[text1, val1], [text2, val2], ...] and do it in one line:

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

Upvotes: 2

Related Questions