How to add next and previous pagination without any gem in Rails?
So! in the controller
class ProductsController < ApplicationController PRODUCTS_SIZE = 20 def index @page = (params[:page] || 0).to_i @products = Product.offset(PRODUCTS_SIZE * @page).limit(PRODUCTS_SIZE) end end
now in the view
<div> <ul class="pager"> <li class="previous <%= @page == 0 ? 'disabled' : '' %>"> <%= link_to_if @page > 0, "<< Previous", products_path(page: @page - 1) %> </li> <li class="next"> <%= link_to "Next >>", products_path(page: @page + 1) %> </li> </ul> </div>
Done!