Wednesday, December 8, 2010

Will Paginate Ajax pagination

I have gone through many articles on how to use Ajax pagination with will_paginate.

Here is how i did & it worked for me,

Step 1 :
Create a helper called remote_link_renderer.rb and use the following code,

class RemoteLinkRenderer < WillPaginate::LinkRenderer
def prepare(collection, options, template)
@remote = options.delete(:remote) || {}
super
end

protected
def page_link(page, text, attributes = {})
@template.link_to_remote(text, {:url => url_for(page), :method => :get}.merge(@remote), attributes)
end
end

Step 2:
Make the view file table in a partial(_units.html.erb),

<div id="units">
<%= render :partial => "units" %>
</div>

Step 3 :

And in the partial _units.html.erb add will paginate code,
<%= will_paginate @units, :renderer => 'RemoteLinkRenderer' , :remote => { :update => 'units'} %>


Step 4 :

In the controller use:

respond_to do |format|
format.js { render :partial => 'units' }
format.html { }
end

Now Pagination works without refreshing the page.

No comments: