Pergunta

I have a form with a date attribute.

Right now I'm using f.date_select :begin_date, discard_day: true but that displays month and year in a each own's select menu respectively.

Is it possible to have both month and year displayed in the same select menu, like below?

2013 January
2013 February
2013 March
2013 April
etc.

Foi útil?

Solução

It's 7 years later, but this is still is still coming up when I Googled for this exact problem.

You can make it work with a standard ActionView::Helpers::FormBuilder#select and feed it the set of month+years by hand

<% the_months = (Date.new(2020,1,1)..Date.new(2020,12,31)).select { |d| d.day == 1 } %>
<%= f.select :start_date, the_months.map { |d| [d.strftime('%b %Y'), d] } %>

Outras dicas

You can have a look to rails API documentation AND date select method for more details.

Your code will be something like this:

<%= f.date_select :date,  { :discard_day => true} %>

I had a similar problem where I wanted just one field with a datepicker where the user could only select a month and a year. Today I found out about the month_field FormHelper which solved the problem for me.

I know it's not an answer to the problem the original poster asked about but hopefully it can help others googling for the same problem.

<%= f.month_field :date %>

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top