Posts

Showing posts from June, 2013

Rails Kaminari - Ajax pagination

Here is a simple way to implement AJAX pagination using Kaminari gem.

Suppose, We are having a Product Model with fields id and name.

So, In the ProductsController,

def index
  @products =  Product.all.page(params[:page]).per(params[:per])
   respond_to do |format|
      format.js
      format.html
    end
end

in view file index.html.haml

  #products
    = render 'products'
  #paginator
    = paginate @products, :remote => true

And create _products.html.haml(this will be rendered from index) with content
%table
    @products.each do |product|
     %tr
       %td= product.name

Finally, We have to create one more  file _index.js.haml with content

$('#products').html("#{escape_javascript(render 'products')}");
$('#paginator').html("#{escape_javascript(paginate(@products, :remote => true)) }");

This post may help you. Please comment on the same, If you guys have any queries.