Programming Ruby on Rails

How to add next and previous pagination without any gem in Rails

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!

Fool Dev

I didn't get trained by the school system like other kids, and when I did concentrate on learning, my mind was cluttered and locked by the programming of the system. © Huey Newton