21

I'm in an app that is on Rails 2.3.8, and need to return an array of month names and numbers to be plugged into an options_for_select statement. What I've got so far is kind of working, but not really. The reason I'm doing things this way is because the select statement needs a prompt, which you can't give options_for_select by default in 2.3.8 (at least to my knowledge).

Here is what I have so far:

@months = [['-', '']]
(1..12).each {|m| @months << [[Date::MONTHNAMES[m], m]]}

So what I'm looking to get returned are options like this:

<option value="1">January</option>
<option value="2">February</option>

However, instead I get:

<option value="January1">January1</option>
<option value="February2">February2</option>

What am I missing?

10 Answers 10

39

Try this!

@months = [['-', '']]
(1..12).each {|m| @months << [Date::MONTHNAMES[m], m]}
4
  • 19
    Yep, too many brackets. You can compress it even more using map, and pass it to options_for_select: options_for_select((1..12).map {|m| [Date::MONTHNAMES[m], m]}). Commented Apr 6, 2011 at 14:28
  • @Douglas: I would upvote your answer if you had left it as one :)
    – iwasrobbed
    Commented Apr 28, 2011 at 14:13
  • This is awesome! And brilliant update by @DouglasFShearer! This is exactly one of the reasons I'm moving to Ruby from Python, lol
    – Dmitry
    Commented Dec 5, 2012 at 22:50
  • In case anyone is looking for how to do this while supporting internationalization: (1..12).map {|m| I18n.l(DateTime.parse(Date::MONTHNAMES[m]), format: "%B")}
    – cman77
    Commented Oct 28, 2016 at 14:41
19
Date::MONTHNAMES.each_with_index.collect{|m, i| [m, i]}
=> [[nil, 0], 
    ["January", 1], 
    ["February", 2], 
    ["March", 3], 
    ["April", 4], 
    ["May", 5], 
    ["June", 6], 
    ["July", 7], 
    ["August", 8], 
    ["September", 9], 
    ["October", 10], 
    ["November", 11], 
    ["December", 12]]

Abbreviated selection with default option

Date::ABBR_MONTHNAMES.compact.each_with_index.collect{|m, i| [m, i+1]}
                     .insert(0, ['Please Select', nil])
=> [["Please Select", nil], 
    ["Jan", 1], 
    ["Feb", 2], 
    ["Mar", 3], 
    ["Apr", 4], 
    ["May", 5], 
    ["Jun", 6], 
    ["Jul", 7], 
    ["Aug", 8], 
    ["Sep", 9], 
    ["Oct", 10], 
    ["Nov", 11], 
    ["Dec", 12]]
1
  • 1
    Extremely minor note: to_a does the same thing as your first map. Commented Feb 11, 2020 at 16:51
11

You can use rails helper select_month, like:

select_month(Date.today)
1
  • How do you use that inside of a form-helper, though? Commented Mar 14, 2017 at 3:05
2

This is my solution for you, in case you work with I18n-based projects, that requires multilingual features:

def month_array
  # Gets the first day of the year
  date = Date.today.beginning_of_year
  # Initialize the months array
  months = {}

  # Iterate through the 12 months of the year to get it's collect
  12.times do |i| # from 0 to 11
    # Get month name from current month number in a selected language (locale parameter)
    month_name = I18n.l(date + i.months, format: '%B', locale: :en).capitalize
    months[month_name] = i + 1
  end
  return months
end

# => {"January"=>1, "February"=>2, "March"=>3, "April"=>4, "May"=>5, "June"=>6, "July"=>7, "August"=>8, "September"=>9, "October"=>10, "November"=>11, "December"=>12}

Regards

2

Here's a sexy way to get the months only if that's all you want:

Date::MONTHNAMES.slice(1..-1).map(&:to_sym)
1

Working for ROR 4

select_month(0 , prompt: 'Choose month')
1

If you want it to be translated to the selected language.

t("date.month_names")
1

Found this nifty way to shift off the first nil but return a new collection of months. You cannot use shift on it since that would try to modify the frozen variable.

Date::MONTHNAMES.drop(1)
1
  • You can use Date::MONTHNAMES.compact if you want to remove nil values
    – RoRFan
    Commented Feb 19, 2021 at 16:46
0

Try it

<% (1..12).each do |month|%>
  <%= link_to t('date.month_names')[month], '#' %>
<% end %>
0
# alternative 1 
Date::MONTHNAMES.compact.zip(1.upto(12)).to_h

# alternative 2
Date::MONTHNAMES.compact.zip([*1..12]).to_h

Outputs:

{"January"=>0, "February"=>1, "March"=>2, "April"=>3, "May"=>4, "June"=>5, "July"=>6, "August"=>7, "September"=>8, "October"=>9, "November"=>10, "December"=>11}

Example:

# using simple_form
f.input :month, collection:  Date::MONTHNAMES.compact.zip(1.upto(12)).to_h

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.