2012-11-16 2 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_indexeach 교체 :

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

i + 1each_with_index 시작하기 때문에 제로가있다.