Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby/Rails中的持久计数器_Ruby_Ruby On Rails 3_Pagination_Each_Counter - Fatal编程技术网

Ruby/Rails中的持久计数器

Ruby/Rails中的持久计数器,ruby,ruby-on-rails-3,pagination,each,counter,Ruby,Ruby On Rails 3,Pagination,Each,Counter,我有下面的代码,它为我提供了一组位置和每个位置的索引 <%= paginate @locations %> <% @locations.each_with_index do |location, index| %> <h1><%= index + 1 %></h1> <h2><%= location.name %></h2> <% end %> 一切正常,我以正确的索引按正

我有下面的代码,它为我提供了一组位置和每个位置的索引

<%= paginate @locations %>

<% @locations.each_with_index do |location, index| %>
  <h1><%= index + 1 %></h1>
  <h2><%= location.name %></h2>
<% end %>

一切正常,我以正确的索引按正确的顺序获取所有元素(访问次数最多,然后是酒吧等):

  • 1宫殿
  • 2巴
  • 3电影院
  • 4餐厅
  • 5气球
我使用Kaminari进行分页,如果我点击第二页显示接下来的5个位置,索引将再次从1开始,但它应该给我6

  • 下一页(应以6继续索引)
  • 1个竞技场(应为6个)
  • 2体育场(应为7)
  • 等等
那么,如何获得一个持续计数器/索引,并在下一页继续

提前谢谢

更新

我提出了以下建议,迄今为止效果很好:

<% if params[:page].nil? || params[:page] == "0" || params[:page] == "1" %>
  <% x = 0 %>
<% else %>
  <% page = params[:page].to_i - 1 %>
  <% x = page * 10 %>
<% end %>

<% @locations.each_with_index do |location, index| %>
  <%= index + x + 1 %>
  ...

...

每个带有索引的\u都是一个普通的ruby方法(我认为是可枚举的),它对分页结构一无所知。您可能需要提供额外的计算,除非Kaminari提供了一些内容(我还没有使用Kaminari)

如果这是will_paginate,我会这样做,也许对你也有用:

<h1><%= index + 1 + ((params[:page] || 0 ) * @items_per_page )  %></h1>


(你必须自己设置@items\u每页)

你能使用记录的id吗<代码>不保证id是连续的