ale*_*enm 0 ruby gem sinatra will-paginate ruby-on-rails-3
场景:我有一个图片表,其中包含数百张照片。我目前正在使用“ will_paginate”对每页100张照片进行分页。我想继续使用'will_paginate',但是我希望分页能够按日进行。
我已经尝试通过使用以下方法,sort_by但是我认为它没有用。
@pics = Picture.paginate(:page => params[:page]).sort_by(&:created_at)
最终目标:
主页仅显示我今天的照片。
现在,如果我单击第2页,则仅显示昨天的照片。
现在,如果单击第3页,它将显示两天前拍摄的照片。
我想继续使用 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
Run Code Online (Sandbox Code Playgroud)
# 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)
Run Code Online (Sandbox Code Playgroud)