2017-01-25 35 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%;"} %> 

这很适合我。谢谢。