0

エラー

undefined method `current' for nil:NilClass
Extracted source (around line #28):

コード

 25: </tr>
 26: <% end %>
 27: </table>
 28: <%= link_to 'Previous page',{:page => @post_pages.current.previous } if    @post_pages.current.previous %>
 29: <%= link_to 'Next page',{:page => @post_pages.current.next } if @post_pages.current.next  %>
 30: <br />

コントローラーコード

  # GET /posts
  # GET /Posts.json
  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @posts }
    end
  end
4

1 に答える 1

1

上記のコメンターの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

于 2012-07-11T02:41:06.997 に答える