在我看来,我想展示所有的标准。但是它应该每页显示10条记录。所以我用will_pagination来处理这个案子。当我点击第一页时,它用S.No显示了标准的前10条记录。但是当我点击第二页时,第11张唱片的S.No显示"1“。但S.No应该是第11记录的11倍。这不管用。会有什么问题?
控制器:
@standards = Standard.select("standards.standard_id, standards.standard_name")
.where("standards.org_id = ?", org_id)
@standards = @standards.paginate(:per_page => 10, :page => params[:page]) 视图:
<% if (@standards != nil && @standards.length > 0) then%>
<% @standards.each.with_index(1) do |standard,index| %>
<tr>
<td> <%= index %></td>
<td> <%= standard.standard_name %></td>
</tr>
<% end %>
<div class="text-right">
<% if @standards != nil then%>
<%= will_paginate @standards, :class => 'pagination-xs', renderer: BootstrapPagination::Rails %>
<%end%>
</div>发布于 2015-05-18 09:41:19
使用param值获取序列号:
<% count = ((params[:page] || 1).to_i - 1) * 10 %>
<% if (@standards != nil && @standards.length > 0) then%>
<% @standards.each.with_index do |standard,index| %>
<tr>
<td> <%= count + index %></td>
<td> <%= standard.standard_name %></td>
</tr>
<% end %>
<div class="text-right">
<% if @standards != nil then%>
<%= will_paginate @standards, :class => 'pagination-xs', renderer: BootstrapPagination::Rails %>
<%end%>
</div>更新
在您的控制器代码中:
@standards.paginate(:per_page => 10, :page => params[:page])您是说每页需要10条记录,并且将页码param设置为params[:page]。
最初,我们没有得到任何params,因为它是第1页。因此,如果没有收到param值,我们必须把它当作1。
params[:page] || 1那样的话,count = (1-1)*10 = 0
假设您在第二页,让您的URL如下所示:
localhost:3000/standards?page=2现在你有了params[:page] = 2
和count = (2 - 1)*10 = 10
因此,10将添加到第二页中每条记录的序列号中。
https://stackoverflow.com/questions/30298166
复制相似问题