12Abr/100
Will_paginate and AJAX
I have been using will_paginate ever since I started on Rails.
It's a great plugin for classic page rendering, but what if you want to use it with AJAX, so it updates a document element (a floating window layer, for instance) with the results instead of reloading the page?
Well, inspired on Redline's blog post (which almost worked for the latest version of the will_paginate plugin), I have the solution.
Create a link renderer. I put in under lib/remote_link_renderer.rb so Rails always loads it on startup.
class RemoteLinkRenderer < WillPaginate::LinkRenderer
def page_link_or_span(page, span_class = 'current', text = nil)
text ||= page.to_s
if page and page != current_page
@template.link_to_remote(text, {:url => url_for(page), :method => :get}.merge(@remote))
else
@template.content_tag :span, text, :class => span_class
end
end
end
On the view, when you are rendering the pager, do it like this:
<%= will_paginate @collection, :renderer => 'RemoteLinkRenderer' , :remote => {:with => ’value’, :update => ‘some_div’} %>
That's it!