2013-07-26 67 views
0

我使用的是選擇輔助標記的形式:Rails的選擇標記輔助選擇沒有對Rails 3.2.14現有值

<%= f.label :male, 'Gender' %> 
<%= f.select :male, [['Girl', 0], ['Boy', 1]] %> 

GirlBoy菜單項在下拉菜單中顯示出來了,但Boy永遠不會選擇,即使在數據庫male設置爲1male實際上是一個schema.rbboolean

如果maletrue,我如何獲得f.select標籤以自動選擇Boy

回答

0

其實你需要調用options_for_select,這是你可以定義選擇的選項。例如:

options = options_for_select [['Girl', 0], ['Boy', 1]], 1 
f.select :male, options 

將輸出選擇標記,其選項和男孩將被選中。

-

您可以在options_from_collection_for_select看看也,最初被設計爲與唱片集一起使用,如:

options_from_collection_for_select @users, :id, :name 

要生成與id的價值和name文本選項。 options_from_collection_for_select基本上可以在任何地方使用

coll = %w[cheese garlic] 
options_from_collection_for_select coll, :to_s, :humanize 

也將工作

+0

我確實需要'options_for_select',我玩過,但問題是帽子我需要使用'resource.male? 1:0「作爲第二個參數到'options_for_select'。我不知道我可以使用'resource.attribute'(使用Devise)。 –