Skip to content

Instantly share code, notes, and snippets.

@jims3ne1
Last active October 11, 2015 21:27
Show Gist options
  • Save jims3ne1/3921771 to your computer and use it in GitHub Desktop.
Save jims3ne1/3921771 to your computer and use it in GitHub Desktop.
pagination setter in the base controller
class Api::BaseController < ActionController::Base
protected
def set_pagination(name, options = {})
scope = instance_variable_get("@#{name}")
request_params = request.query_parameters
url_without_params = request.original_url.slice(0..(request.original_url.index("?")-1)) unless request_params.empty?
url_without_params ||= request.original_url
page = {}
page[:first] = 1 if scope.total_pages > 1 && !scope.first_page?
page[:last] = scope.total_pages if scope.total_pages > 1 && !scope.last_page?
page[:next] = scope.current_page + 1 unless scope.last_page?
page[:prev] = scope.current_page - 1 unless scope.first_page?
pagination_links = []
page.each do |k,v|
new_request_hash= request_params.merge({:page => v})
pagination_links << "<#{url_without_params}?#{new_request_hash.to_param}>;rel=\"#{k}\">"
end
headers[:Link] = pagination_links.join(",")
end
end
@mrbrdo
Copy link

mrbrdo commented Sep 27, 2013

Line 19 you should remove > at the end and line 21 it should be response['Links']

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