How do I add a counter to an .each
loop? Is there any easy way of doing this?
I tried the below, but it does not seem to work.
<% @album.each do |e| %>
<%= e %> #my_counter_does_not_work :)
<%= link_to e.name, :controller => "images", :action => "album", :album_id => e.id, :album_name => e.name %>
<% end %>
Use each_with_index
: the index will automatically be your counter (but note it starts at 0 and not 1):
<% @album.each_with_index do |e, index| %>
<%= link_to e.name, :controller => "images", :action => "album", :album_id => e.id, :album_name => e.name %>
<% end %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With