will_paginate 可以按天排序吗

发布于 2024-12-24 19:15:20 字数 436 浏览 1 评论 0原文

场景:我有一个包含数百张照片的图片表。我目前正在使用“will_paginate”对每页 100 张照片进行分页。我想继续使用“will_paginate”但我希望分页由日期驱动。

我已经使用sort_by尝试了以下操作,但我认为这不是在职的。

@pics = Picture.paginate(:page => params[:page]).sort_by(&:created_at)

最终目标:
首页仅显示今天的图片。
现在,如果我点击第 2 页,它只会显示昨天的照片。
现在,如果我点击第 3 页,它会显示两天前拍摄的照片。

我想继续使用 will_paginate

SCENARIO: I have a Pictures table that contains hundreds of photos. I'm currently using 'will_paginate' to paginate 100 photos per page. I would like to continue using 'will_paginate' BUT I want the pagination to be driven by the day.

I have tried the following by using sort_by but I don't think it's working.

@pics = Picture.paginate(:page => params[:page]).sort_by(&:created_at)

END GOAL:
Home page shows me today's pictures only.
Now if I click on page 2 it shows me yesterdays pictures only.
Now if I click on page 3 it shows me pictures taken from two days ago.

I would like to continue using will_paginate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕色琉璃 2024-12-31 19:15:20

您不能使用按日期按组/范围进行分页。您需要自己做这件事。不过这并不是非常困难,你可以这样做:

# pictures_controller.rb

def index
  @date             = Date.parse(params[:date]) rescue Date.today
  @pictures         = Picture.where(:created_at => @[email protected]_day.at_midnight)

  @total_pictures   = Picture.count
  @current_pictures = @pictures.size
end

# pictures/index.html.haml

- @pictures.each do |picture|
  = #....

= "Showing #{@current_pictures} pictures for #{@date}."
= "There are a total of #{@total_pictures} pictures."

= link_to 'View Previous day photos', pictures_url(:date => @date.prev_day)
- if @date.past?
  = link_to 'View Next day photos', pictures_url(:date => @date.next_day)

You cannot use will paginate by group/scope by date. You'll need to do this yourself. It's not terribly difficult though, you could do something like this:

# pictures_controller.rb

def index
  @date             = Date.parse(params[:date]) rescue Date.today
  @pictures         = Picture.where(:created_at => @[email protected]_day.at_midnight)

  @total_pictures   = Picture.count
  @current_pictures = @pictures.size
end

# pictures/index.html.haml

- @pictures.each do |picture|
  = #....

= "Showing #{@current_pictures} pictures for #{@date}."
= "There are a total of #{@total_pictures} pictures."

= link_to 'View Previous day photos', pictures_url(:date => @date.prev_day)
- if @date.past?
  = link_to 'View Next day photos', pictures_url(:date => @date.next_day)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文