2014-09-05 75 views
1

我試圖使用select_tag選擇默認選項。下面是我收集select_tag中的選定值

@model_names = { 
    'Fixed' => 'fixed', 
    'Variable' => 'variable', 
    'LIBOR' => 'libor' 
} 

這裏是select_tag

select_tag "second_model", options_for_select(@model_names, 'LIBOR') 

我都嘗試 'LIBOR' 和 'LIBOR'。甚至{'LIBOR'=>'libor'}作爲options_for_select的第二個參數。但它不起作用。當集合是散列時,如何設置默認選項?

//編輯

哦,它適用於'libor'和...禁用JavaScript。我使用selectBoxIt插件,它不適用於它。任何人都知道如何選擇默認值使用Rails + selectBoxIt jQuery插件?

+0

它看起來像您可以在選擇標籤的HTML屬性使用'數據selectedtext'。請參閱:http://gregfranko.com/jquery.selectBoxIt.js/#HTML5DataAttributeOptions – pdobb 2014-09-05 14:52:59

回答

0

如果您嘗試將您的變量設置爲數組而不是哈希,可能會更好地設置默認選項。然後你可以做些像options_for_select([['Default','default'] [@ model_names]])。在你的下拉菜單中,它將默認爲默認值,並在下拉菜單中顯示你的收藏夾。你可以在這裏找到一些幫助鏈接。

RoR select_tag default value & options

http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select

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