Pagination with Kaminari

I haven’t written any blog posts for a while. I was either busy hacking Objective-C or just being sick instead …

Here’s one Ruby thing I did today. Nothing fancy, just pagination using Kaminari

After adding the Kaminari gem in the Gemfile and bundle install, we start from the controller. All we needed to do is to chain the page(params[:page]) method call after the ordered scope

def typed_requests
  @requests = Request.ordered.page(params[:page])
end

Next up, adding the pagination HTML to the view

= paginate @requests, :remote => true

To finish it off, some coffeescript to listen on the new pager links

jQuery ->
  $('.pagination a').live 'click', (e) ->
    e.preventDefault()
    unless $(this).parent().attr('class').match(/active|disabled/)
      $.get $(this).attr('href'), (data) ->
        $(".container").html(data)

A side note, yesterday, Symfony master Fabien Potencier wrote a post PHP is much better than you think. It stirred up quite some controversy. As someone who worked with PHP for a good 6 to 7 years before moved on doing Ruby, my response is simple. Putting aside how good or bad PHP and its entire ecosystem is, the ease of use from Ruby and all these Ruby gems beats PHP hands down …

Published: 2012-07-05
blog comments powered by Disqus