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气球
- 下一页(应以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是连续的