2017-02-20 27 views
1

我在.erb文件中的函数,并在它,我有以下几点:如何更改顺序的两个项目中。每次在红宝石

<%select id ="dropdown"> 
<option disabled selected value="select"> - select - </option> 
<% @variable.each do |x| %> 
<%= "<option value='#{x['code']}'>#{x['description']}   </option>".html_safe %> 
<% end %> 
<%/select> 

但是我需要有两个项目(TUA和CRL)显示在顶部,其余部分以字母顺序显示(当前是),因此它应该是。

<%select id ="dropdown"> 
<option disabled selected value="select"> - select - </option> 
<% @variable.each do |x| %> 
<% next if x['code'] == 'TUA' or x['code'] == 'CRL' %> 
<%= "<option value='#{x['code']}'>#{x['description']}   </option>".html_safe %> 
<% end %> 
<%/select> 

我该怎么做?我对它进行了硬编码,我不想那样。

回答

1

不知道如何声明@variable,但如果尝试将其安排在控制器中,该怎么办?

@variable = [] 
@variable << Variable.find_by(code: 'TUA') 
@variable << Variable.find_by(code: 'CRL') 

@variables = Variable.all.order('code DESC') 
@variables.each do |x| 
    if x.code != 'TUA' && x.code!= 'CRL' 
    @variable << x 
    end 
end 
+0

@variable在控制器中定义,它是一个Web服务返回调用。 – Remon87

+0

@Remon87做了上述工作? – gwalshington

+0

不幸的是它没有工作。此外,我需要在erb文件中修复,而不是在控制器中。我仍然试图弄清楚。 – Remon87