2014-03-13 29 views
-1

我有一個下拉的Ruby on Rails的下面,即:選定值頁面刷新後丟在Ruby on Rails的

<table> 
<tr> 
    <td align="center"> 
     <%= f.select(:Interest,options_from_collection_for_select(@students, "id", "student_mentor_subjects"), {},:id => "DDL_Students", :style => "width:160px;") %> 
    </td> 
</tr> 
</table> 

而且學生是被填充f.select和下面的表是架構學生表:

id | student_mentor_subjects 
1 | ijk 
2 | mno 
3 | pqr 

當我從上面f.select選擇一個值,然後單擊搜索按鈕,然後頁面刷新,選擇的值丟失了,我怎麼能留住f.select刷新頁面後選擇的值。 請幫助我,謝謝。

回答

2

從API:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_from_collection_for_select

options_from_collection_for_select(collection, value_method, text_method, selected = nil)

第四個參數,以options_from_collection_for_select是默認值。在你的情況下,你需要從params中設置它,然後回落到一個特定的默認值,如果沒有參數(這將是第一次加載頁面的情況)。

我不知道從參數中讀取哪個值,因爲你沒有發佈表單的其餘部分,但是如果你看看你的日誌,你應該看看你想讀取哪個值。因此,它可能

 <%= f.select(:Interest,options_from_collection_for_select(@students, "id", "student_mentor_subjects", params[:search][:interest] || @students.first.id), {},:id => "DDL_Students", :style => "width:160px;") %> 
+0

它給我一個錯誤「無法轉換成符號整數」 – user88

+0

我曾經說過,只是一個例子:你有沒有貼什麼是存儲在@students或你的參數是什麼。 –

+0

我編輯了我的帖子,親切地看到和吸取我 – user88

相關問題