S.No 在分页中单击第“2”页时没有刷新 link

S.No not getting refreshed when clicking on page "2" in pagination link

我想在我的视图中显示所有标准。但它应该每页显示 10 条记录。所以我用 will_pagination 来处理这种情况。当我点击第一页时,它显示了前 10 条带有 S.No 的标准记录。但是当我点击第二页时,第11条记录的S.No显示“1”。但是第 11 条记录的 S.No 应该是 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>

使用参数值获取序列号:

<% 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 条记录,并且您已将页码参数设置为 params[:page]

最初我们没有得到任何 params,因为它是第 1 页。所以如果没有收到参数值,我们必须将其视为 1。

params[:page] || 1

那样的话count = (1-1)*10 = 0

考虑到你在第二页,让你的URL像这样:

localhost:3000/standards?page=2

现在你有 params[:page] = 2

count = (2 - 1)*10 = 10

所以10将被添加到第二页每条记录的序号。