Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Humanize rails select helper

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.

like image 501
Slick23 Avatar asked Dec 27 '22 05:12

Slick23


1 Answers

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.

like image 171
KL-7 Avatar answered Jan 06 '23 07:01

KL-7