sergserg
sergserg

Reputation: 22264

Generate a select element for years in Ruby

I need a drop down list that just shows the years from 1990 to 2014.

I've tried:

<%= select_tag "year", [1900..2014] %>

But I get:

<select id="year" name="year">[1900..2014]</select>

Any suggestions?

Upvotes: 0

Views: 136

Answers (2)

girasquid
girasquid

Reputation: 15526

Use () instead of [], like this:

<%= select_tag "year", options_for_select((1900..2014).to_a) %>

The expression [1900..2014] represents an array whose only element is a range. Calling to_a on a 1..10 range returns [1,2,3,'...','etc'].

Edit: oops, select_tag, unlike the f.select you use insde form_for blocks doesn't automatically convert array into option tags, so you need to use options_for_select. @dimuch just noted that.

Upvotes: 1

dimuch
dimuch

Reputation: 12818

Try to add options_for_select

<%= select_tag "year", options_for_select(1900..2014) %>

Upvotes: 1

Related Questions