Skip to content

andresbravog/api_pagination_headers

 
 

Repository files navigation

#api_pagination_headers Build Status Dependency Status

api_pagination_headers is a gem that adds pagination info to a Link response header. This is considered to be the best practice for REST APIs and is currently used by GitHub. It also adds the total number of records into a X-Total-Count header.

Example:

Link: <http://example.com/posts?page=2&per_page=10>; rel="next", <http://example.com/posts?page=2&per_page=10>; rel="last"
X-Total-Count: 11

##Installation

Include in your Gemfile:

gem 'api_pagination_headers'

Depending on will_paginate at the moment. Support for rails-api is built in.

##Usage

In the controller:

class PostsController < ApplicationController
  after_action only: [:index] { set_pagination_headers(:posts) }

  def index
    @posts = Post.all.paginate(per_page: params[:per_page], page: params[:page])
    respond_with @posts
  end
end

##License Copyright © 2014 Richard Käll. Licensed under the MIT license.

About

Link header pagination for APIs.

Resources

License

Stars

Watchers

Forks

Packages

No packages published