Using will_paginate in Rails to Submit an AJAX Form
I’m using will_paginate for a Rails project. It works great, but I wanted to use it for a sidebar that performs searches and displays the results. It’s not too hard to write a custom link renderer that will submit the will_paginate link using Ajax, but my situation was a bit different: I wanted will_paginate to inject the page number into the search form and submit the form using Ajax.
Here’s what I ended up doing: writing a link renderer that calls a
JavaScript function that injects the page number into the form and calls the
form’s onsubmit
function (which then submits the form using Ajax).
The code. First the Haml, which is in a partial that gets rendered on a number of pages:
Next, the will_paginate renderer, in the file
app/helpers/sidebar_search_link_renderer.rb
. The “#” is the href value,
and the onclick attribute calls a JavaScript funtion.
Finally, the JavaScript function which I put in
public/javascripts/application.js
: