-1

我從rails開始,我不能在rails語法中編寫它。我可以像這樣使用變量嗎?我怎樣才能比較2個變量與真假格式?Rails - 有條件的語法

<% n = 0 %> 
<% @clas.each do |f| %> 
    <% if f.clas_id != n %> 
     <div> 
      <h2> <%= f.clas_id%></h2> 
     </div> 
    <% else %> 
     <% if f.clas_id = n %> 
     <div class = "title"> 
      <h3><%= f.name%></h3> 
      <table> 
       <tr> 
        <th>PA</th> 
        <th>Disim.</th> 
       </tr> 
       <tr> 
        <td><%= f.prec_id %></td> 
        <td><%= f.dis_id %></td> 
       </tr> 
      </table> 
     </div> 
     <% else %> 
      <% n = f.clas_id %><%= n %> 
    <% end %> 
    <% end %> 
<% end %> 
+0

你得到的錯誤是什麼? – webster

+0

1)我看不到'@ clas'被定義在任何地方2)你使用的是clas_id和clase_id - 那些是一樣的嗎? 3)更好地看看你的條件:有'如果f.clas_id!= n'和對面的一個不能包含更多條件簡單,因爲不會有任何離開! – Zelenka

+0

1)'@clas'是控制器 - '@clas = ACp.clas'誰交易範圍。 'scope:clas,lambda {order(「a_cps.clas_id DESC」)。group(「a_cps.clas_id」)}'。 2)是的,它是一樣的。 – Chili

回答

0

嘗試這樣:

<% @clas.each do |f| %> 
    <% if f.clas_id != n %> 
    <div> 
     <h2><%= f.clas_id %></h2> 
    </div> 
    <% elsif f.clas_id == n %> 
    <div class = "title"> 
     <h3><%= f.name %></h3> 
     <table> 
     <tr> 
      <th>PA</th> 
      <th>Disim.</th> 
     </tr> 
     <tr> 
      <td><%= f.prec_id %></td> 
      <td><%= f.dis_id %></td> 
     </tr> 
     </table> 
    </div> 
    <% else %> 
    <% n = f.clase_id %><%= n %> 
    <% end %> 
<% end %> 

林不知道你的努力與<% n = 0 %>

你不寫別的事情做,如果像你這樣的語句在Ruby中,你寫:

<% elsif %> 
+0

林試圖給一個變量來說明_id,而_id是相同的,做的時候,當改變把標題和改變變量再次相同。 – Chili

+0

如果你想永久地改變ID,你需要在模型內部應用這個邏輯 –

+0

我只想檢查哪一個是與該行對齊的clas_id(foreing key)。把berofe作爲 – Chili