2017-01-25 99 views
0

我完成了grouped_options_for_select。我想要一個提示。如果在該組下沒有選擇,則不應該有空間。我嘗試使用下面的代碼。
控制器:grouped_options_for_select in rails 4

@grouped_options = @subjects.inject({}) do |options, product| 
    (options[product.subject_name] ||= []) << [product.module_name, product.subject_module_id] 
    options 
end 

觀點:

<div id="subject_module_drp_div"> 
    <%= f.select :subject_module_id, grouped_options_for_select(@grouped_options), 
        {:class=>"form-control select_modules",:style=>"width:100%;"} %> 
</div> 

我的選項,選擇喜歡,

語法:
緊張
語音

科學:

數學:
代數
Trignomentry

這裏科學沒有選項來選擇。所以數學緊接着科學。科學和數學之間沒有空間。是否有任何選項可以在grouped_options_for_select中執行此操作。以及如何提供即時價值並獲得已選擇的選項。
微笑&謝謝。

回答

0

我嘗試使用此問題進行更多搜索並獲得解決方案。對不起,這裏的問題徹底check.And之前,我的回答是,
控制器:

(options[product.subject_name] ||= []) << if product.module_name != nil then [product.module_name, product.subject_module_id] else ["Nil", :disabled => true] end 

查看:

<%= f.select :subject_module_id, grouped_options_for_select(@grouped_options, selected: @faculty_profile.subject_module_id), 
     {:prompt => 'Select Module'}, {:class=>"form-control select_modules",:style=>"width:100%;"} %> 

這很適合我。謝謝。