S.点击页面" 2"没有得到刷新。在分页链接

时间:2015-05-18 08:16:30

标签: ruby-on-rails will-paginate

我想在我的视图中显示所有标准。但它应该每页显示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>

1 个答案:

答案 0 :(得分:1)

使用参数值获取序列号:

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

请考虑您在第二页,并让您的网址如下:

localhost:3000/standards?page=2

现在你有params[:page] = 2

count = (2 - 1)*10 = 10

因此10将被添加到第二页中每条记录的序列号。