prasannaboga
prasannaboga

Reputation: 1014

Date select tag with month-year in the same option

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?

Upvotes: 1

Views: 3276

Answers (1)

prasannaboga
prasannaboga

Reputation: 1014

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) %>

Upvotes: 2

Related Questions