2011-12-30 74 views
6

我有一個模型問題,它有一個名爲category的列。使用rails f.select helper預先選擇一個選項?

我有一個數組,列出了所有有效類別:Question.categories

<%= form_for(@question) do |f| %> 
    <%= f.select :category, options_for_select(Question.categories) %> 
    #... 
<% end %> 

說我有一個名爲@currentlySelectedCategory變量。

現在我該如何告訴rails來預先選擇與@currentlySelectedCategory相匹配的下拉菜單中的選項?

回答

7
<%= form_for(@question) do |f| %> 
    <%= f.select :category, options_for_select(Question.categories, @currentlySelectedCategory) %> 
    #... 
<% end %> 

但由於您使用的是form_for,我會認爲導軌會選擇問題類別。

+0

太棒了!這工作。謝謝! – 2011-12-30 22:48:40

+1

很高興我可以幫忙,但你應該閱讀這些文件的這種東西;) – Robin 2011-12-30 22:50:46

+1

我閱讀文檔選擇和相關的方法,這是沒有幫助的。我沒有意識到,我應該看看文件的options_for_select,而不是:P – 2011-12-30 23:28:14

相關問題