2017-02-20 26 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