导轨select_tag选定的值
对于下面给出的代码,我想保持与传递的值select的select框。
但是这不起作用:
@yrs =[2011,2010,2009,2008] <%= select_tag 'year', options_for_select([["Select" , "" ]] + @yrs.to_a,:selected=>2011) %>
请告诉我如何去做。
谢谢
删除:selected=>
部分。
句法:
options_for_select(@options, @selected_options)
用法:
options_for_select(1..5, 3) # creates a range 1..5 , with 3 as selected by default
<%= select_tag "page_type", options_for_select(@page_type.collect{ |u| [u.data_name, u.id]}, :selected=>@page.page_type), {:class =>"select_combobox",:onchange=>"reset_form(this.id,'page_type_msg');"} %>
这对我有用:)
只是澄清@M塔里克阿齐兹答案:
你的代码应该是这样的:
@yrs =[2011,2010,2009,2008] <%= select_tag 'year', options_for_select([["Select" , "" ]] + @yrs.to_a,2011) %>
select标签的一般格式是:
<%= select_tag 'year', options_for_select(:collection, :selected) %>