與雷

2011-08-18 62 views
7

月分頁我想分頁按月職位,我在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 %> 

問題:

  1. 分頁不按月工作,我想在一個頁面中顯示一個月的所有結果,通過params [:month] = 2或params [:month] = Feb取代params [:page]
  2. 我認爲「2011年8月」,而不是1,2
  3. 環月和今年一樣,當你轉到前面,而在「2011年1月」,它會轉到「2010年12月」

回答

7

我想這是不是一個真正的事的分頁。處理查詢的params [:month]值與頁面偏移切換有些不同。你可能不需要一個分頁庫。

如何簡單地創建這樣的鏈接?

控制器:

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

觀點:

<% 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 -%> 

當然你也可以在需要時此查詢與正常分頁結合起來。但在這種情況下,分頁鏈接不應與月份鏈接混在一起。