0

I'm trying to populate a formtastic select menu using an Array.

model

PAYMENT_METHODS = %w[creditcard check money-order cash western-union]

views

<%= f.input :payment_methods, :as=>:select, :collection => User::PAYMENT_METHODS%>

It works but, this is how it appear now.

<select>
<option value="creditcard">creditcard</option>
<option value="western-union">western-union</option>
</select>

Instead I want it to look like:

<select>
<option value="creditcard">Credit Card</option>
<option value="western-union">Western Union</option>
</select>

How can I get this to work?

1 Answer 1

1

I wasn't able to test it, but I think you can do it this way.

%w[Credit\ Card Check Money \Order Cash Western\ Union]

From the Programming Ruby docs.

Updated:

After reading back through the select examples on the formtastic Github page, I believe you can do this. As before, it is untested.

 <%= f.input :payment_methods, :as=>:select, :collection => { "Credit Card" => "creditcard", "Check" => "check", "Money Order" => "money-order", "Cash" => "cash" "Western Union" => "western-union" > 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but, I'd like to store the value as credit-card in database while showing Credit Card as the display name for option item.
Ah, I didn't realize you wanted something different for the database. I've updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.