2012-12-15 63 views
0

在我的索引行動的控制器,我有;問題與分頁和索引行動

def index 
    @documents = current_user.documents.all if current_user 
end 

我在current_user的末尾添加的任何內容我遇到一個錯誤。例如,我嘗試添加will_paginate,它只是將.paginate(:per_page => 5, :page => params[:page])添加到索引操作的末尾,並將<%= will_paginate @documents %>添加到視圖。

一旦我將.paginate(:per_page => 5, :page => params[:page])添加到索引方法的末尾,就像這樣;

def index 
    @documents = current_user.documents.all if current_user.paginate(:per_page => 5, :page => params[:page]) 
end 

我得到一個NoMethodError。任何人都知道如何解決這個問題?

回答

1

嘗試

def index 
    @documents = current_user.documents.paginate(:per_page => 5, :page => params[:page]) if current_user 
end 
+0

它的工作原理!那麼沒有.all的問題是否重要? – user1658756

+0

.paginate僅適用於ActiveRecord :: Relation,並且.all將關係轉換爲數組 –

+0

嗯。謝謝! – user1658756

0

你叫錯了對象的方法,這裏是正確的:

@documents = current_user.documents.paginate(:per_page => 5, :page => params[:page]) if current_user 
+0

現在我已經得到了錯誤的'未定義的方法'PAGINATE」爲#' – user1658756

+0

我想我畢竟回答了一個Mongoid背景:)。 –

+0

啊,我明白了。哈哈:) – user1658756