I want to build a select
input from an array (and not a collection of models), with SimpleForm, and have different classes for each option
s.
I would have hoped that this would work:
f.input :method, collection: [
["option text", "option_value_1", { class: "class_name_1" }],
["option text 2", "option_value_2", { class: "class_name_2" }]
]
The problem with that is it will generate:
<select>
<option value="option text" class="class_name_1">option text</option>
<option value="option text 2" class="class_name_2">option text 2</option>
</select>
How can I do what I want (value should be "option value") with simple form?
This appears to be a limitation when using collections, see the author of SimpleForm's explanation here. He recommends a workaround of the form:
f.input :method, :as => :select do
f.select :method, [['option text', 'option_value_1', {"class" => "class_name_1"}], ['option text 2', 'option_value_2', {"class" => "class_name_2"}]]
end
You also can pass array of arrays as an argument
= f.input :status, collection: [['option text', 'option_value_1', {"class" => "class_name_1"}], ['option text 2', 'option_value_2', {"class" => "class_name_2"}]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With