我添加了“选择气体”作为默认值,但是问题是当我对@Gases变量进行排序时,“选择气体”进入了下拉菜单的中间。我希望“选择气体”成为第一项,即使@Gases已排序。我怎样才能做到这一点?谢谢
Gas变量
@Gases = ['Carbon Dioxide [CO2]','Ethanol [CH3CH2OH]','Hydrogen [H2]','Hydrogen Sulfide [H2S]', 'Nitrogen Dioxide [NO2]', 'Carbon Monoxide [CO]', 'Ethene [Ethylene C2H6]', 'Other']
搜索
<%= f.select :gas_analyte, @Gases, {include_blank: 'Select Gas', size:15},{class: "gas_search"} %><br>
答案 0 :(得分:1)
尽管您的代码看起来不错,但是如果您有一个数组并且希望特定项始终排在最前面,则可以使用Array#prepend
来完成工作:
list = [:a, :h, :w, :c, :d, :b, :e]
list.sort.prepend(:z)
=> [:z, :a, :b, :c, :d, :e, :h, :w]
因此,您可能有:
@gases = ['Carbon Dioxide [CO2]','Ethanol [CH3CH2OH]','Hydrogen [H2]','Hydrogen Sulfide [H2S]', 'Nitrogen Dioxide [NO2]', 'Carbon Monoxide [CO]', 'Ethene [Ethylene C2H6]', 'Other']
@gases.sort.prepend('<Select Gas>')
=> ["<Select Gas>", "Carbon Dioxide [CO2]", "Carbon Monoxide [CO]", "Ethanol [CH3CH2OH]", "Ethene [Ethylene C2H6]", "Hydrogen Sulfide [H2S]", "Hydrogen [H2]", "Nitrogen Dioxide [NO2]", "Other"]