我在我的 Rails 应用程序中使用 will_paginate:https://github.com/mislav/will_paginate
在我的帖子 Controller 中:
@posts = Post.order("created_at DESC").paginate(:page => params[:page], :per_page => 10)
在我看来:
<ol>
<%= render (@posts) %>
</ol>
<%= will_paginate @posts :next_label => "next" , :previous_label => 'previous' %>
在我的部分帖子中:
<li><%= link_to post.title, post_path(post) %></li>
在第一页上,这为我提供了 1-10 的列表顺序列表,但是当我转到下一页时,我得到了 1-10 的不同列表。我如何获得它以便在下一页上显示 11-20?
Started GET "/?page=2" for 127.0.0.1 at 2014-03-30 22:11:06 -0400
Processing by PostsController#index as HTML
Parameters: {"page"=>"2"}
最佳答案
您应该在您的 View 中尝试以下操作:--
<%- page = params[:page].to_i%>
<ol start = "#{page > 1 ? (((page -1) * 10) + 1) : 1}">
<%= render (@posts) %>
</ol>
<%= will_paginate @posts :next_label => "next" , :previous_label => 'previous' %>
您应该指定 ol 标签的起始编号。
关于ruby-on-rails - 将在有序列表上分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752211/