2012-11-16 65 views
0

在顯示錶記錄列表時,我有一列顯示每個記錄的ID(以查看錶中有多少記錄) 唯一的問題是例如,如果記錄號3被刪除,則該id也被刪除(由於唯一性)。顯示記錄數的時間順序

而不是顯示id,有沒有辦法可以列出1,2,3,4,5等查看列表時? 在表中?

這是我的表:

<center> 
    <p class="recordnumber"><%= pluralize(@ranches.size, 'Ranch') %> found<p> 

    <%= render :partial => "shared/newlink" %> 

    <table class="listing" summary="Ranches"> 
    <tr> 
    <th>#</th> 
    <th>Ranch Name</th> 
    <th>Address</th> 
    <th>Telephone</th> 
    <th>Actions</th> 
    </tr> 
    <% @ranches.each do |ranch| %> 
    <tr class="<%= cycle('odd', 'even') %>"> 
    <td><%= ranch.id %></td> 
    <td><%= ranch.name %></td> 
    <td><%= ranch.address_line_1 %>,</br><%= ranch.town_city %>,</br><%= ranch.postcode %></td> 
    <td><%= ranch.telephone %></td> 
    <td> 
    <%= link_to("Edit", {:action => 'edit', :id => ranch.id}, :class => 'buttons') %> 
    <%= link_to("Delete", {:action => 'delete', :id => ranch.id}, :class => 'buttons') %> 
    </td> 
    </tr> 
    <% end %> 
    </table> 

回答

1

是。與each_with_index這樣的替換each

<% @ranches.each_with_index do |ranch, i| %> 
    <tr class="<%= cycle('odd', 'even') %>"> 
    <td><%= i + 1 %></td> 
    <td><%= ranch.name %></td> 
.... 

i + 1是因爲有開始each_with_index零。