在发布此问题之前,我在这里尝试了类似问题的解决方案:
will_paginate function not rendering page links
而不是< - 1 2 3 ... N - >,我看到消息“获取更多产品......”。
//控制器
1. @posts是ActiveRecord :: Relation
2. 6记录结果,所以期待3页
@posts = User.joins(entries: [{storage: :vote}, :category])
.where("votes.count > ?", 0)
.select("users.username AS username,
storages.id AS storage_id,
storages.title AS title,
storages.content AS content,
votes.count AS votes,
categories.category_name AS category_name")
.order("votes.count DESC")
@posts = @posts.page(params[:page]).per_page(2)
//查看
<% @posts.each do |post| %>
...
...
<%end%>
<%= will_paginate @posts %>
http://localhost:3000/?page=1
我看到“获取更多产品......”
对于http://localhost:3000/?page=2
也“获取更多产品......”
但对于http://localhost:3000/?page=3
我看到了上一页1 2 3下一页→。
为什么只在最后一页进行分页?
我正在使用RoR 4.0.0
Gemfile: gem 'will_paginate', '~> 3.0.5'
答案 0 :(得分:0)
应该是
@posts = @ posts.paginate(params [:page],per_page:2)
快速查看 http://skatara.blogspot.com/2015/02/pagination-in-rails-using-willpaginate.html