1
votes

Some Rails form helpers allow us to easily put select fields for a date input. The thing here is that individual date fields, day-month-year, come separated by default. From a UI perspective, this can be awkward.

Is it possible to put, say, month and year each in only one option, as such:

<select>
 <option value="01/05/2012">May - 2012</option>
 <option value="01/06/2012">Jun - 2012</option>
 ... 
 <option value="01/04/2013">Apr - 2013</option>
</select>

While it is definitely possible to do by hand, can we make use of the DateHelpers, eg select_month or date_select and all their magic to do this?

1
date_select with :discard_day => trueYuri Barbashov
api.rubyonrails.org/classes/ActionView/Helpers/… use options: :discard_day => true, :use_short_month => true, :date_separator => " - "Sector

1 Answers

2
votes

In controller

@date = Date.today
@months = []
(0..11).each do |m|
  @months << [@date.next_month(m).strftime("%b %Y"), @date.next_month(m)]
end

In erb

<%= select_tag "month_year", options_for_select(@months) %>