Rails:显示记录数的时间顺序 [英] Rails: Display chronological order of number of records

查看:140
本文介绍了Rails:显示记录数的时间顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在rails中显示表记录列表时,我有一个列,显示每个记录的id(以便查看表中有多少条记录)
唯一的问题是,例如,如果record数字3被删除,然后id也被删除(由于唯一性)。



而不是显示id,是有一种方法,我可以列出1,2,3 ,4,5等当查看列表?
在表中?



这是我的表:

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

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

< table class =listingsummary =Ranches>
< tr>
< th>#< / th>
< th>牧场名称< / th>
< th>地址< / th>
< th>电话< / th>
< th>操作< / 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 ,如下所示:

 <%@ 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 从零开始。


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).

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?

This is my 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>

解决方案

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>
....

The i + 1 is there because each_with_index starts at zero.

这篇关于Rails:显示记录数的时间顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆