Limit number of pages in will_paginate

六眼飞鱼酱① 提交于 2019-11-30 21:20:36

I think it's better to submit the parameter :total_entries to method paginate:

@posts = Post.paginate(:page => params[:page], :per_page => 30, 
                       :total_entries => 1000)

will_paginate will generate links only for the number of pages needed to show 1000 results.

You can also verify that the requested page belongs to the interval:

if params[:page].to_i * 30 <= 1000
  @posts = Post.paginate(:page => params[:page], :per_page => 30, 
                         :total_entries => 1000)
end

Moreover, by submitting the parameter :total_entries, you avoid the sql COUNT query that will_paginate normally runs to retrieve the total number of entries.

if params[:page].to_i * 30 <= 1000
  @posts = Post.paginate(:page => params[:page], :per_page => 30)
end

I found the best solution was to do this:

@results = Model.search(...)
if @results.total_pages >= (1000/Model.per_page)
  class << @results; def total_pages; 1000/Model.per_page; end end
end

Where the 1000 is preferably not hardcoded :). This gets you the correct behaviour of the will_paginate view helper for free,

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!