will_paginate可以按天排序 [英] will_paginate can it order by day
问题描述
我尝试了以下方法 sort_by
但我认为它不起作用。
@pics = Picture.paginate (:page => params [:page])。sort_by(&:created_at)
END GOAL:强烈>
主页仅显示今日的图片。
现在,如果我点击第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
#pictures / index.html.haml
- @ pictures.each do | picture |
=#....
=显示#{@ date}的#{@ current_pictures}图片。
=总共有#{@ total_pictures}张照片。
= link_to'查看前一天的照片',pictures_url(:date => @ date.prev_day)
- if @ date.past?
= link_to'查看下一天的照片',pictures_url(:date => @ date.next_day)
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
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 => @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)
这篇关于will_paginate可以按天排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!