我想按月对帖子进行分页,所以我在 Post 模型中添加了以下范围
class Post
include Mongoid::Document
include Mongoid::Timestamps
scope :by_month, lambda {|end_date| Post.order_by(:created_at => :asc).where(:created_at.gte => (end_date.to_date.beginning_of_month), :created_at.lte => (end_date.to_date))}
end
在我的控制器中,我放了
def show
@posts = Post.by_month(Time.now).page(params[:page]).per(20)
end
在视图中
<%= paginate @posts, :theme => 'month_theme' %>
<%= render @posts %>
问题:
- 分页不按月工作,我想在页面中显示一个月的所有结果,将 params[:page] 替换为 params[:month]=2 或 params[:month]=Feb
- 如何查看“2011 年 8 月”而不是 1,2
- 循环月份和年份,就像您在“2011 年 1 月”中转到上一个时一样,它将转到“2010 年 12 月”