简体   繁体   中英

formtastic select menu using an Array

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM