對於下面給出的代碼,我想保持與傳遞的值選擇的選擇框。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塔裏克·阿齊茲提供答案,作品,請接受它。 –