0
我有兩個模型,一個使用卡,另一個使用相關的獎勵程序。我將它們全部放在一個表中,並且在某些列中使用了條件if語句,但我無法弄清楚爲什麼if語句將我的列鎖定。我發佈了兩個例子,一個可行,一個不可行。我需要第二個工作增加一些額外的功能在循環表中使用if語句rails
此示例適用
<table>
<tr>
<th>Card</th>
<th>General Rewards</th>
<th>Gas Amount</th>
<th>Movies Amount</th>
<th>Museums Amount</th>
<th>Theme Park Amount</th>
<th>Restaurant Amount</th>
<th>Department Store Amount</th>
</tr>
<% @cards.each do |card| %>
<tr>
<td><%= card.name %></td>
<td><%= card.general_rate %> </td>
<% card.rewards.each do |category| %>
<% if category.name.downcase == "gas" %>
<td><%= category.threshold_check(@gas) %></td>
<% end %>
<% if category.name.downcase == "movies" %>
<td><%= category.threshold_check(@movies) %></td>
<% end %>
<% if category.name.downcase == "museums" %>
<td><%= category.threshold_check(@museums) %></td>
<% end %>
<% if category.name.downcase == "theme parks" %>
<td><%= category.threshold_check(@theme_parks) %></td>
<% end %>
<% if category.name.downcase == "restaurants" %>
<td><%= category.threshold_check(@restaurants) %></td>
<% end %>
<% if category.name.downcase == "department stores" %>
<td><%= category.threshold_check(@department_stores) %></td>
<% end %>
<% end %>
</tr>
<% end %>
</table>
這增加了額外的列到最後
<table>
<tr>
<th>Card</th>
<th>General Rewards</th>
<th>Gas Amount</th>
<th>Movies Amount</th>
<th>Museums Amount</th>
<th>Theme Park Amount</th>
<th>Restaurant Amount</th>
<th>Department Store Amount</th>
</tr>
<% @cards.each do |card| %>
<tr>
<td><%= card.name %></td>
<td><%= card.general_rate %> </td>
<% card.rewards.each do |category| %>
<td><%= category.name.downcase == "gas" ? category.threshold_check(@gas) : 0 %></td>
<td><%= category.name.downcase == "movies" ? category.threshold_check(@movies) : 0 %></td>
<td><%= category.name.downcase == "museums" ? category.threshold_check(@museums) : 0 %></td>
<td><%= category.name.downcase == "theme parks" ? category.threshold_check(@theme_parks) : 0 %></td>
<td><%= category.name.downcase == "restaurants" ? category.threshold_check(@restaurants) : 0 %></td>
<td><%= category.name.downcase == "department stores" ? category.threshold_check(@department_stores) : 0 %></td>
<% end %>
</tr>
<% end %>
</table>
我沒有看到一個'<% end %>'標籤用於'<%card.rewards.each do | category | %>'這裏,但是這應該會產生一個模板錯誤。 – cdesrosiers