2011-11-28 36 views
0

呈現的HTML:select_tag options_for_select,默認值沒有得到設置

<select id="version_filter" name="version_filter"> 
<option value="2">Current Version</option> 
<option value="1">1</option> 
<option value="0">0</option> 
</select> 

通過警報到我的網頁的select_tag

<%= select_tag "version_filter", 
     options_for_select(@version_select, @selected_version.to_s) %> 

我看是否@selected_version甚至存在:

alert("<%= @selected_version.to_s %>"); 

它確實。 @selected_version是0,但是當頁面加載時,下拉選擇事件顯示「當前版本」作爲選定的值。

Rails並沒有抱怨語法,所以,我怎樣才能得到默認選擇的第一個值?

+1

是version_select'整數或字符串在'@值?如果它們的值比'@ selected_version'的字符串值不相等。 –

+0

@version_select是一個2d的整數數組。我會嘗試刪除.to_s UPDATE:沒有工作,行爲與問題相同。 – NullVoxPopuli

+0

更新2:事實證明我必須做.to_i o.o – NullVoxPopuli

回答

1

我需要確保@selected_version與我試圖將其匹配到@version_select中的項目類型相同。

由於@version_select,在這種情況下,是整數的2D陣列,

@selected_version needs to have .to_i instead of .to_s