Rails AJAX Pagination with will_paginate plugin

Hi Guys,

If you are using will_paginate plugin for pagination then sometimes you may have a requirement for AJAX pagination.

I am damn sure that it is much simple instead of your thinking that how hard to do that.

First go through this link: http://weblog.redlinesoftware.com/2008/1/30/willpaginate-and-remote-links

It describes very well AJAX Pagination.

However I did some custom changes for my application.

In my view file I wrote:

<%= will_paginate must_have_item, :renderer => ‘RemoteLinkRenderer’, :remote => {:with => “‘must_have_items=true'”, :update => ‘must_have_gallery_whole’} %>

In above code,

  • must_have_item is an item array.
  • RemoteLinkRenderer is a file that you are supposed to include it in helper file(DO NOT CHANGE ANY CONTENT OF IT.)
  • must_have_items=true is a parameter that I passed with this request
  • must_have_gallery_whole is an id of div that updates after every AJAX request

Now In controller I wrote,

if request.xml_http_request?()
if params[:must_have_items]

In above code,

  • request.xml_http_request?() will get the AJAX request
  • params[:must_have_items] will be true as I passed “must_have_items=true” in view file.

So, write whatever you want after this two if conditions.

This is the way AJAX Pagination is working.

As I told you earlierĀ  it is very easy.

Now cheerssssss…….