开发者

Rails pagination by date

I'm looking to implement pagination based on a date.

I am using a named_scope to pull a note for today but I'd like to add functionality to be able to have a previous link that will show a note for yesterday.

If I happened to be on the page for yesterday I'd also like to be able to have a link that will show the day before yesterday and a link that will go forward one day, back to today, and so on...

I'm wondering if there are any helpers or plugins that would achieve this simply or if s开发者_如何学Comeone could point me in the right direction (I'm struggling to know what to even search for).

Cheers

Tom


That's not exactly pagination 'cause eventually you could have more than n (where n = per_page records) records count for a given date, I think you should write a previous_day and next_day helper using the yesterday and tomorrow methods, something like:

def previous_day_for(date)
  link_to "previous", your_filter_path(:date => date.yesterday.to_s(:db))
end

def next_day_for(date)
  link_to "next", your_filter_path(:date => date.tomorrow.to_s(:db))
end

Hope it helps you!


Have you thought about using the will_paginate plugin and setting the page size to 1? If you sort the data by date, then you could have your pagination working with 1,2,3. The links can then be customized, or you could create your own helpers that change links using the page_link function

will_paginate: http://github.com/mislav/will_paginate

page_link documentation: http://gitrdoc.com/rdoc/mislav/will_paginate/b3b0f593ea9b1da13a64bc825dfe17b6bbc2828b/classes/WillPaginate/LinkRenderer.html#M000159

I hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜