Select options in ruby

1

I would like to know how I can manually enter the options of a select, I have been using the following:

<%= f.collection_select :establecimiento_id, Establecimiento.order(:nombre), :id, :nombre, include_blank: true %>

but that is useful to select data from a table, I want to create one with the months, will it be necessary to create a table with the months or is there a way to enter in the code what I want?

    
asked by Bryan 09.02.2017 в 15:02
source

1 answer

1

You could create an arrangement with the months of the year:

@months = ["january", "february", "march", ... ]

and use the builder f.select as follows:

<%= f.select :month, options_for_select(@months.each_with_index.map { |m, i| [m, i] }) %>

This would generate something like

...
<option value="0">january</option>
<option value="1">february</option>
...

o If you expect a particular value for each month you could start from a Hash

@months = {"jan": "Enero", "feb": "Febrero" ... }

and the builder would be:

<%= f.select :month, options_for_select(@months.map { |k, v| [v, k] }) %>
    
answered by 12.02.2017 в 14:17