Monthly pagination with kaminari

I want to break the posts by month, so I added the following area in the Post model

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 

In my controller I put

 def show @posts = Post.by_month(Time.now).page(params[:page]).per(20) end 

In sight

 <%= paginate @posts, :theme => 'month_theme' %> <%= render @posts %> 

Problems:

  • pagination does not work for months, I want to show the whole month result on the page, replacing params [: page] with params [: month] = 2 or params [: month] = Feb
  • How to view "August 2011" instead of 1.2
  • The month and year of the cycle, for example, when you switch to the previous time in "January 2011", it will be "December 2010"
+7
source share
1 answer

I suppose this is not a pagination issue. Handling the params [: month] value for a query is something other than switching page offsets. You may not need a swap library for this.

How easy is it to create these links like this?

controller:

 @posts = Post.by_month(Time.parse(params[:month]) || Time.now) 

View:

 <% Post.only(:created_at).map {|p| p.created_at.to_date.beginning_of_month}.uniq.sort.each do |m| -%> <%= link_to_unless_current m, :month => m %>&nbsp; <% end -%> 

Of course, you can combine this query with a normal pagination if necessary. But page links should not be confused with month links in this case.

+7
source

All Articles