Kevin Whitaker
Kevin Whitaker

Reputation: 13425

Rails - return an array of months for a select tag

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?

Upvotes: 21

Views: 17695

Answers (10)

CTS_AE
CTS_AE

Reputation: 14783

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)

Upvotes: 1

boyd
boyd

Reputation: 351

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

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

Upvotes: 2

mld.oscar
mld.oscar

Reputation: 438

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

Upvotes: 2

Roger
Roger

Reputation: 7612

# 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

Upvotes: 0

Luiz Picolo
Luiz Picolo

Reputation: 113

Try it

<% (1..12).each do |month|%>
  <%= link_to t('date.month_names')[month], '#' %>
<% end %>

Upvotes: 0

Philip
Philip

Reputation: 7166

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

t("date.month_names")

Upvotes: 1

heroku_slave
heroku_slave

Reputation: 11

Working for ROR 4

select_month(0 , prompt: 'Choose month')

Upvotes: 1

rafaelkin
rafaelkin

Reputation: 425

You can use rails helper select_month, like:

select_month(Date.today)

Upvotes: 11

Sandip Ransing
Sandip Ransing

Reputation: 191

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]]

Upvotes: 19

krunal shah
krunal shah

Reputation: 16339

Try this!

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

Upvotes: 39

Related Questions