2013-05-08 79 views
0

我創建一個下拉的用戶喜歡 -添加選項組標籤下拉

= f.association :user, :collection => current_user.company.users.order('first_name'), 
         :include_blank => "Select a approver", 
         :label => false, 

它創建了一個下拉我想要什麼。 現在,我想這將不可選

我想米跌落的樣子

label field 
all users list goes after label 

所有用戶前加一個標籤,這個標籤字段將是不可選的,即用戶無法選擇這個標籤。 有什麼想法?

回答

0

您可以手動添加一個選項是這樣的:

:collection => { "My new label" => ""}.merge(current_user.company.users.order('first_name')) 

你可能想提取到一個輔助方法,但我會離開,給你。

新選項現在有一個空值相關聯,所以你可能會阻止該選項的選擇,在模型驗證:

class MyModel < ActiveReceord::Base 
    validates :user, :presence => true 

    # ... 
end 

現在對於具體的選項禁用,則可以添加選項:disabled => [...]選擇助手,將在HTML disabled attribute添加到選擇您指定的選項:

= f.association :user, 
    :collection => :collection => { "My new label" => ""}.merge(current_user.company.users.order('first_name')), 
    :include_blank => "Select a approver", 
    :label => false, 
    :disabled => [""] 
+0

感謝您的答覆。但是這也不能正常工作,它顯示以下錯誤 無法將ActiveRecord :: Relation轉換爲哈希 – user1875926 2013-05-08 10:24:40

+0

再次感謝...我正在使用禁用選項。但我想要的是一種非選擇的標籤添加到下拉 – user1875926 2013-05-08 10:25:56

+0

儘管它的工作原理,它會添加一個空白值選項字段地掉了下來,如:include_blank – user1875926 2013-05-08 11:01:20