首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示记录数的时间顺序

显示记录数的时间顺序
EN

Stack Overflow用户
提问于 2012-11-17 01:15:27
回答 1查看 279关注 0票数 0

在rails中,当显示表记录的列表时,我有一列显示每条记录的id (以便查看表中存在多少条记录),唯一的问题是,例如,如果删除了3号记录,那么id也会被删除(由于唯一性)。

有没有一种方法可以在查看列表时列出1,2,3,4,5等,而不是显示id?在一张桌子里?

这是我的表格:

代码语言:javascript
复制
  <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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-17 01:17:36

是。将each替换为each_with_index,如下所示:

代码语言:javascript
复制
<% @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从零开始。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13421305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档