18

I installed ActiveAdmin successfully:

My gemfile code:

source 'https://rubygems.org'

 gem 'rails', '3.2.10'

 # Bundle edge Rails instead:
 # gem 'rails', :git => 'git://github.com/rails/rails.git'

 gem 'sqlite3'


 # Gems used only for assets and not required
 # in production environments by default.
 group :assets do
   gem 'sass-rails',   '~> 3.2.3'
   gem 'coffee-rails', '~> 3.2.1'

   # See https://github.com/sstephenson/execjs#readme for more supported runtimes
   # gem 'therubyracer', :platforms => :ruby

   gem 'uglifier', '>= 1.0.3'
 end

 gem 'jquery-rails'

 gem 'twitter-bootstrap-rails'

 gem 'activeadmin'

  # gem "meta_search",    '>= 1.1.0.pre'
 gem "spud_photos"
 gem 'devise'

 gem 'cancan'
 gem 'rolify'

and i did this:

 bundle
 rails g active_admin:install
 rake db:migrate
 rails g active_admin:resource product

I linked some models to ActiveAdmin.

Error after clicking in the dashboard on the product link:

 undefined method `per' for #<ActiveRecord::Relation:0x4d15ee0>
ubuseral
  • 427
  • 1
  • 3
  • 14
  • This helped me: http://tech-brains.blogspot.in/2012/11/kaminari-willpaginate-incompatibility.html – RAJ Jun 03 '15 at 07:10

8 Answers8

41

Active Admin need kaminari pagination If you want to use will paginate, you can make alias for will paginate functions to match kaminari one:

# config/initializers/will_paginate.rb
if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        alias_method :per, :per_page
        alias_method :num_pages, :total_pages
      end
    end
  end
end

module ActiveRecord
  class Relation
    alias_method :total_count, :count
  end
end

And this one worked for me.

mohamed-ibrahim
  • 10,837
  • 4
  • 39
  • 51
26

This one helped me:

 if defined?(WillPaginate)
   ActiveSupport.on_load :active_record do
     module WillPaginate
       module ActiveRecord
         module RelationMethods
           def per(value = nil) per_page(value) end
           def total_count() count end
         end
       end
       module CollectionMethods
         alias_method :num_pages, :total_pages
       end
     end
   end
 end
drKreso
  • 1,030
  • 10
  • 16
  • Don't forge to mention to add this code to a file and stick it in the config/initializers folder. I would name the file will_paginate.rb but use anything you'd like that would remind you what it's for. – JCC Mar 17 '14 at 22:23
  • and don't forget to restart the server – truongnm Aug 14 '17 at 07:33
  • This solution solve my problem, Thanks! work for 2018! My `Gemfile` have `gem 'will_paginate', '~> 3.1.1'`, no `gem kaminari` (will_paginate conflict with kaminari) But in `Gemfile.lock` I saw `administrate` is using `kaminari`. Anyway, I put these code into `config/initializers/will_paginate.rb`, and it work.! – NamNamNam Mar 13 '18 at 09:20
11

You can create an Initializer for Kaminari, like this:

Kaminari.configure do |config|
  config.page_method_name = :per_page_kaminari
end

In my experience, I had to restart the server to make it work. That's all.

hmartinezd
  • 1,188
  • 8
  • 11
4

I'm using Ruby 2.1.5p273 and Rails 4.1.8. I encountered the same problem. @mohamed-ibrahim's answer solved the error underfined method 'per', but got another error

Showing c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/bundler/gems/activeadmin-06bf79c58216/app/views/active_admin/resource/index.html.arb where line #2 raised: wrong number of arguments (0 for 1)

Adding alias_method :total_count, :count fixed it.

if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        alias_method :per, :per_page
        alias_method :num_pages, :total_pages
        alias_method :total_count, :count
      end
    end
  end
end
Sithu
  • 4,752
  • 9
  • 64
  • 110
4

The above answers do not work anymore. An updated answer was given here by @zitoon:

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

I tried it myself. Works.

Chidozie Nnachor
  • 872
  • 10
  • 21
4

If you are using kaminari and will_paginate together, you will definitely face this error. In short, kaminari and will_paginate are incompatible to each other. If you are using rails_admin (which uses kaminari for pagination) and also using will_paginate, you will need to add the following code to one of the initializers under config directory or you can create a new file, let say with name'will_paginate' add the code, and place it into initializers directory.

if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        def per(value = nil) per_page(value) end
        def total_count() count end
      end
    end
    module CollectionMethods
      alias_method :num_pages, :total_pages
    end
  end
end
Felender
  • 61
  • 5
0

I had the same issue and switched from WillPaginate to Kaminari for my app.

It is a simple change: paginate(page:1,per_page:10) becomes page(1).per(10)

I guess it depends how deeply willPaginate is enmeshed with your app.

tomf
  • 449
  • 4
  • 13
0

This worked for me:

initializers/will_paginate.rb

if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        alias_method :per, :per_page
        alias_method :num_pages, :total_pages
        alias_method :total_count, :total_entries
      end
    end
  end
end
joeyk16
  • 1,357
  • 22
  • 49