Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 696
Kevin Whitaker Avatar asked Apr 06 '11 13:04

Kevin Whitaker


1 Answers

Try this!

@months = [['-', '']]
(1..12).each {|m| @months << [Date::MONTHNAMES[m], m]}
like image 58
krunal shah Avatar answered Oct 02 '22 18:10

krunal shah