ruby-on-railsrubyjsonrubygemswill-paginate

will_paginate JSON support?


I wonder if anyone could please tell me if will_paginate can support JSON out of the box or if this has to be hacked?

I would like to add page data to the JSON responses while will_paginate manages pagination.


Solution

  • Something in the lines of:

    @posts = Post.paginate :page => params[:page]
    
    respond_to do |format|
      format.json {
        render :json => {
          :current_page => @posts.current_page,
          :per_page => @posts.per_page,
          :total_entries => @posts.total_entries,
          :entries => @posts
        }
      }
    end
    

    Once you figure out what format you want, you can always define a to_json method on a WillPaginate::Collection.