I am building a simple service(API) that will return json reponse of users objects using Ruby on Rails. Since it returns an array of objects i want to add pagination functionality (:next_page , :prev) as follows.
"nextpage":www.domain.com/users?page=2&per_page=5
Currently i have a restful route that return url www.domain.com/users.How do i reconstruct the url in my index action and how do i make use of limit and offset in this case to make the pagination to work? I want to return 5 items in every page.Sorry i know it is a newbie question, it is my first time creating a service, if somebody could show me how to do the nextpage i would appreciate and i will implement prev by myself. I have tried looking for tutorials to do this but all of them they don’t show the logic in the action/method.Thank you
Note: I dont want to use will_paginate, i just need to build simple paginate
Just append your page and page_per to your links to whatever action. Assume you have standard restful resource based routing, i.e.
config/routes.rb:
so you’d have the following route generated
GET ‘/users’ => ‘users#index’
if you send this url to your rails app:
‘/users?page=2&per_page=5’
You will still get the index action, but you’ll have two extra parameters , you can do this:
controllers/users_controller.rb