will_paginate 默认显示最后一页(保持时间顺序) [英] will_paginate show the last page by default (keeping chronological order)
问题描述
我正在构建一个索引页面,我需要在其中显示具有日期的结果,按时间顺序排列(从最早的日期到最新的日期),但默认情况下我需要始终显示最后一页.
I'm building an index page where I need to display results which have a date, ordered chronologically (from the earliest date to the newest one), but I need to always show the last page by default.
我不能对日期使用 DESC 命令,因为它会破坏我需要这个的全部目的 =(.
I can't use a DESC order for the date because it would defeat the whole purpose of what I need this for =(.
TL;DR:我需要显示一个分页的日志列表,按日期升序排序,但从最后一页开始.这可以用 will_paginate 实现吗?谢谢!
推荐答案
这是我最终得到的解决方案:
This is the solution I finally arrived to:
if params[:page]
@collection = @q.result(distinct: true).paginate(:page => params[:page], :per_page => 30)
else
last_page = @q.result(distinct: true).paginate(:page => params[:page], :per_page => 30).total_pages
@collection = @q.result(distinct: true).paginate(:page => last_page, :per_page => 30)
end
它并不花哨,它需要一些重构,也许可以使用记忆来避免两次调用 paginate
方法,但嘿它有效.如果您尝试直接访问某个页面,它会照常呈现,但如果您不指定页面,则默认将您带到最后一个页面.
It's not fancy, it needs some refactoring and perhaps can use memoization to avoid calling the paginate
method twice, but hey it works. If you are trying to access a page directly, it is rendered as usual, but if you don't specify a page, you are taken to the last page by default.
这篇关于will_paginate 默认显示最后一页(保持时间顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!