Humanize the rails to choose an assistant

My model has the following:

PRODUCTSTATES = %w[published coming_soon in_development cancelled] 

I use this to populate a drop-down list on a form, and I'm trying to use humanize to make the list beautiful, but can't seem to get it.

  <%= f.select :status, Product::PRODUCTSTATES %> 

Product::PRODUCTSTATES.humanize , obviously, does not work and does not convert to a string before starting work.

+4
source share
1 answer

You can pass an array like

 [['caption1', 'value1'], ['caption2', 'value2']] 

to select , and it will generate smth, like

 <select> <option value="value1">caption1</option> <option value="value2">caption2</option> </select> 

In your case, you can do this:

 <%= f.select :status, Product::PRODUCTSTATES.map { |s| [s.humanize, s] } %> 

You will receive 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 more details.

+9
source

Source: https://habr.com/ru/post/1397295/


All Articles