Auto increment value in each cycle

In code:

<% @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 %>

      

I am new to rails. I want to show all sentences in a numbered list, I cannot use the database table because it is id

not ok. For example:

  • 30% OFF on orders of Rs. 2000

  • 13% OFF on orders of Rs. 1000

How can I achieve this?

+3


source to share


1 answer


The easiest way, if you don't want to use each_with_index

, then you can define a variable @count=0

and display, since the loop will increment by 1

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

      



each_with_index way:

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

      

+6


source







All Articles