Reputation: 1684
rails 6.1
, ruby 3.1.1
On a form (form_for
) I have a drop-down select box using options_for_select
in my :sort_order
field. It takes a hash and is currently showing colors as the options text, which is not very intuitive to the user. I could switch it around and show the numbers as the select text, but that still might not be super helpful to the user. I want to combine the key/val in the hash items and show that in the option text.
<% sort_hash = {'platinum' => 1, 'diamond' => 2, 'gold' => 3, 'silver' => 4, 'bronze' => 5} %>
<tr>
<td>Sort Order:</td>
<td>
<%= f.select(:sort_order, options_for_select(sort_hash), :prompt => "Select a Number") %>
</td>
</tr>
# this produces:
<select name="living_muay_thai_badge[sort_order]" id="living_muay_thai_badge_sort_order"><option value="">Select a Sort Order</option>
<option value="1">platinum</option>
<option value="2">diamond</option>
<option value="3">gold</option>
<option value="4">silver</option>
<option value="5">bronze</option>
</select>
What I want to do is have it show the key: value
together in the option text like this:
<select name="living_muay_thai_badge[sort_order]" id="living_muay_thai_badge_sort_order"><option value="">Select a Sort Order</option>
<option value="1">1: platinum</option>
<option value="2">2: diamond</option>
<option value="3">3: gold</option>
<option value="4">4:silver</option>
<option value="5">5: bronze</option>
</select>
I haven't found any threads online that show how to do this with my hash in the options_for_select
, and anything I try doesn't work. I can rework the hash if that makes this easier.
Help?
Thanks in advance.
Upvotes: 1
Views: 47