每个循环中的自动增量值

时间:2014-09-18 11:01:11

标签: ruby-on-rails ruby-on-rails-4

在代码中:

<% @offers.each do |offer|%>
  <%= offer.percentageOff%> <b>% OFF on order of</b>
  <%= image_tag('1407951522',:size=>'10x10',:alt=>'Rs.')%>
  <%= offer.amountforDiscount%>
  <%= button_to 'Delete',{:action=>'destroy',:id=>offer.id},class: "" %>
<% end %>

我是铁杆新手。我想在编号列表中显示所有商品,我无法使用数据库表,因为id不符合要求。例如:

  1. 订购Rs.2000

  2. 30%OFF
  3. 订购Rs.1000

  4. 13%折扣

    我如何实现这一目标?

1 个答案:

答案 0 :(得分:7)

最简单的方法,如果你不想使用each_with_index那么你可以定义变量@count=0并显示,因为循环发生它将增加1

<% @count = 0 %>
<% @offers.each do |offer|%> 
<%= @count += 1 %> #I guess you want this to show Sr.No
...... # your code
<% end %>

each_with_index 方式:

<% @offers.each_with_index do |offer, index|%> 
 <%= index + 1 %> # index starts from 0, so added 1 to start numbering from 1
  ...... # your code
<% end %>