ruby-on-railsruby-on-rails-4html-selectview-helpersdatefield

Can I display both (and only) month and year in a date_select helper tag?


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.


Solution

  • 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] } %>