在rails中,当显示表记录的列表时,我有一列显示每条记录的id (以便查看表中存在多少条记录),唯一的问题是,例如,如果删除了3号记录,那么id也会被删除(由于唯一性)。
有没有一种方法可以在查看列表时列出1,2,3,4,5等,而不是显示id?在一张桌子里?
这是我的表格:
<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>
发布于 2012-11-17 01:17:36
是。将each替换为each_with_index,如下所示:
<% @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从零开始。
https://stackoverflow.com/questions/13421305
复制相似问题