How do I get a counter fo a rails 3 “each do” loop?

how do i add a counter to a .each loop? Any easy way of doing this? I tried the below but it does not seem to work. Thanks!

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

Answers


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

Need Your Help

iOs: Why are IBOutlets not wired up after init], but are after viewDidUnload: has been called

iphone ios interface-builder

I have a simple UINavigationViewController which when a certain item is selected creates a modal view that has an UIImageView (PostcardViewController below) inside it. However, if I call

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.