I have the following in my model:
PRODUCTSTATES = %w[published coming_soon in_development cancelled]
I'm using that to populate a drop-down in a form, and I'm trying to use humanize
to make the list look pretty, but can't seem to get it.
<%= f.select :status, Product::PRODUCTSTATES %>
Product::PRODUCTSTATES.humanize
obviously doesn't work, nor does converting to a string before hand.
You can pass an array like
[['caption1', 'value1'], ['caption2', 'value2']]
to select
helper and it'll generate smth like
<select>
<option value="value1">caption1</option>
<option value="value2">caption2</option>
</select>
In your case you can do like that:
<%= f.select :status, Product::PRODUCTSTATES.map { |s| [s.humanize, s] } %>
You'll get humanized versions of the statuses displayed on the page and the original (non-humanized) versions will be sent to the server when the form is submitted.
See select
and options_for_select
docs for more information.
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