0

我需要一个仅显示 1990 年至 2014 年的下拉列表。

我试过了:

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

但我得到:

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

有什么建议么?

4

2 回答 2

1

使用()代替[],像这样:

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

该表达式[1900..2014]表示一个数组,其唯一元素是一个范围。调用1..10to_a范围返回.[1,2,3,'...','etc']

编辑:哎呀,select_tag,不像f.select你使用 insdeform_for块不会自动将数组转换为选项标签,所以你需要使用options_for_select. @dimuch 刚刚注意到这一点。

于 2013-01-18T19:28:49.330 回答
1

尝试添加options_for_select

<%= select_tag "year", options_for_select(1900..2014) %>
于 2013-01-18T19:53:06.253 回答