我需要一个下拉列表,其中只显示1990年至2014年的年份。
我试过了:
<%= select_tag "year", [1900..2014] %>
但我明白了:
<select id="year" name="year">[1900..2014]</select>
有什么建议吗?
答案 0 :(得分:1)
使用()
代替[]
,如下所示:
<%= select_tag "year", options_for_select((1900..2014).to_a) %>
表达式[1900..2014]
表示一个数组,其唯一元素是范围。
在 1..10 范围内拨打to_a
会返回[1,2,3,'...','etc']
。
编辑: oops,select_tag,与f.select
不同,你使用insde form_for
块不会自动将数组转换为选项标记,因此你需要使用{{1 }}。 @dimuch只是注意到了。
答案 1 :(得分:1)
尝试添加options_for_select
<%= select_tag "year", options_for_select(1900..2014) %>