情景:我有一张图片表,其中包含数百张照片。我目前正在使用'will_paginate'为每页分页100张照片。我想继续使用'will_paginate'但是我希望分页能够被白天驱动。
我使用sort_by
尝试过以下操作,但我认为它不起作用。
@pics = Picture.paginate(:page => params[:page]).sort_by(&:created_at)
结束目标:
主页只显示今天的图片。
现在,如果我点击第2页,它只显示昨天的图片
现在,如果我点击第3页,它会显示我两天前拍摄的照片。
我想继续使用will_paginate
答案 0 :(得分:4)
您不能按日期按组/范围使用will paginate。你需要自己做。但是,这并不是非常困难,你可以这样做:
# pictures_controller.rb
def index
@date = Date.parse(params[:date]) rescue Date.today
@pictures = Picture.where(:created_at => @date.at_midnight..@date.next_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)