Skip to content

Instantly share code, notes, and snippets.

@drfeelngood
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drfeelngood/91195aad5b39b49c76a2 to your computer and use it in GitHub Desktop.
Save drfeelngood/91195aad5b39b49c76a2 to your computer and use it in GitHub Desktop.
Rails API pagination with kaminari using http headers
def respond_with_paginated(collection)
  %w( current_page
      total_pages
      total_count
      prev_page
      next_page
      first_page?
      last_page?
  ).each do |method_name|
    field_name  = "X-Kaminari-#{method_name.titleize.gsub(/\s/, '-')}"
    field_value = collection.send(:"#{method_name}").to_s
    response.headers[field_name] = field_value
  end
  response.headers['X-Kaminari'] = "true"
  respond_with(collection)
end
@drfeelngood
Copy link
Author

Herm, I just learned about the Link HTTP header. RFC-5988 is probably a better solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment