I am using will_paginate
gem in my ROR
project to show the records in pages.
I want the next page to be loaded without reloading the whole
Hey if you want to paginate products then try this:
app/controllers/products_controller.rb
def index
@products = Product.paginate(page: params[:page], per_page: 10)
end
views/products/index.html.erb
<%= render 'products' %>
views/products/_products.html.erb
<% @products.each do |product| %>
# your code
<% end %>
<%= will_paginate @products %>
views/products/index.js.erb
$('.sort_paginate_ajax').html("<%= escape_javascript(render("products"))%>")
assets/javascripts/application.js
$(function() {
$(".sort_paginate_ajax th a, .sort_paginate_ajax .pagination a").on("click", function(){
$.getScript(this.href);
return false;
});
});
So first we are calling paginate method on Product for all products, here an offset will be set according to params[:page]
and limit will be set by per_page
options.
Also we are rendering products
partial which will be rendered every time when other page is opened.
In partial call on @products
that you want, and then will_paginate
method is applied on
@products
, it also creates params[:page]
that is used in controller.
Now in response to ajax request render content of div
having class sort_paginate_ajax
with the partial that we created.
Also to make request ajax request, on document load capture script of all a
tags in div.sort_paginate_ajax
, and return false.
Now pages will be called with ajax request
I hope this would help you.