breaking into the unknown…

pagination missing rails_admin view

Leave a comment

Recently, I have integrated, the current version of rails_admin 0.4.9 and found that, everything working fine except, the pagination link. The pagination link not appearing on any of the index view of the rails_admin. I tried hard to google the solution, but failed and finally posted my problem here on stack overflow. But, since no answer coming from stack overflow, I tried to dig deeper in the problem myself. So this is how I followed

first thing first, check the code, which render the rails_admin index view. my target file is rails_admin-0.4.9/app/views/rails_admin/main/index.html.haml  of the rails_admin gem. On the first look, itself I got the problem, they have put the pagination within if else condition, in their this recent commit

if @objects.respond_to?(:total_count)

.....pagination code...


......without pagination code......


Infact, this change has resulted in a known issue for rails_admin reported here . The thread also have the solution. So what is happening due to the above change. The code is trying to check, if the object have total_count method defined on it or not. The gem owner has introduced this change to prevent “undefined method total_count” . unfortunately, it solve the error at the cost of escaping pagination. So now, we know the cause. The Solution is to define total_count method on the object . The objects here is returned from will_paginate gem. so we are going to add the total_count method to it through monkey patching . monkey patching is nothing but adding new methods or overriding the existing one, by maintaining same hierarchy of class and module in your application. Say, to solve our problem, we will monkey patch will_paginate module as below.

create a file with any name say will_paginate_patch.rb in /config/intializer folder of your app, and add below line to it.

if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        def per(value = nil)
        def total_count()
    module CollectionMethods
      alias_method :num_pages, :total_pages

so, if you see the /lib/will_paginate/active_record.rb file of will_paginate gem, you can see that, they already have per_page and count method defined in it, but there is no total_count or per method in it, so the above patch will add the method and solve our problem.

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s