Vitali Zakharoff
Vitali Zakharoff

Reputation: 353

Number of each row.

I have in my database 3 records and i want that they looks like: if I using for (each)

<% @records.each do |record| %>
  1. record1
  2. record2
  3. record3

Upvotes: 7

Views: 3660

Answers (4)

JRL
JRL

Reputation: 77995

Just wrap it in an ol, that way the numbering is dynamic:

<ol> 
   <% @records.each do |record| %>
     <li><%= record %></li>  
   <% end %>
</ol>

Upvotes: 2

Markus Proske
Markus Proske

Reputation: 3366

Take a look at each_with_index:

<% @records.each_with_index do |record, index| %>

Upvotes: 1

Matt Greer
Matt Greer

Reputation: 62027

You probably want each_with_index. something like:

<% @records.each_with_index do |record, i| %>
   <%= (i+1) %>. <%= record.foo %> <br />
<% end %>

Upvotes: 13

Matteo Alessani
Matteo Alessani

Reputation: 10412

you can use each_with_index:

<% @records.each_with_index do |record, i| %>

  #your code

<% end %>

Upvotes: 7

Related Questions