显示记录数量的时间顺序 [英] Display chronological order of number of records
本文介绍了显示记录数量的时间顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在rails中显示表记录列表时,我有一列显示每条记录的id(以便查看表中存在多少条记录)
唯一的问题是例如if record数字3被删除,然后id也被删除(由于唯一性)。
In rails when displaying a list of table records, I have a column which displays the id of each record (so as to see how many records exist in the table) The only problem is for example is if record number 3 is deleted, then the id is also removed (due to uniqueness).
有没有一种方法可以列出1,2,3 ,4,5等查看列表?表中
?
Instead of displaying the id, is there a way I can list 1,2,3,4,5 etc when viewing the list? in a table?
这是我的表:
<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>
推荐答案
是。用 each_with_index
替换每个
,如下所示:
Yes. Replace each
with each_with_index
like this:
<% @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
从零开始。
这篇关于显示记录数量的时间顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文