上記のコメンターの1人が言及しているように、will paginate gemを使用することをお勧めします。
次に、次のように実装します (これらは gem ページの例です...):
## perform a paginated query:
@posts = Post.paginate(:page => params[:page])
# or, use an explicit "per page" limit:
Post.paginate(:page => params[:page], :per_page => 30)
## render page links in the view:
<%= will_paginate @posts %>
あなたの特定の例では、次のようになります。
def index
@posts = Post.paginate(:page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @posts }
end
end
そして、あなたの見方はより簡単になります:
<%= will_paginate @posts %>
いくつかの詳細に取り組む必要がありますが、それは物事をずっと簡単にします.
背景を説明する 2 つの Railscast を次に示します。
http://railscasts.com/episodes/174-pagination-with-ajax/
http://railscasts.com/episodes/240-search-sort-paginate-with-ajax