对于下面给出的代码,我想保持与传递的值选择的选择框。rails select_tag选定的值
但是,这并不工作:
@yrs =[2011,2010,2009,2008]
<%= select_tag 'year', options_for_select([["Select" , "" ]] + @yrs.to_a,:selected=>2011) %>
请告诉我如何去了解它。
谢谢
对于下面给出的代码,我想保持与传递的值选择的选择框。rails select_tag选定的值
但是,这并不工作:
@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
只是为了澄清@M塔里克·阿齐兹回答:
您的代码应该是这样的:
@yrs =[2011,2010,2009,2008]
<%= select_tag 'year', options_for_select([["Select" , "" ]] + @yrs.to_a,2011) %>
的选择标记的一般格式为:
<%= select_tag 'year', options_for_select(:collection, :selected) %>
我有一个整数作为键值,就像使用字符串作为选定值。你在哪里用to_a我需要使用to_i。感谢您指点我正确的方向。这适用于我:<%= select_tag(:map_set_priority_filter,options_for_select(MapSet.priority_filters.collect {| priority | [priority.name,priority.id]},@ map_set_priority_filter.to_i))%> – John
<%= 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塔里克·阿齐兹提供答案,作品,请接受它。 –