will_paginate JSON support?

user427165 picture user427165 · Jan 15, 2011 · Viewed 12.8k times · Source

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.

Answer

mislav picture mislav · Jan 16, 2011

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.