Rails: activeadmin, undefined method `per' for #

后端 未结 8 1307
暖寄归人
暖寄归人 2021-02-02 18:15

I installed ActiveAdmin successfully:

My gemfile code:

source \'https://rubygems.org\'

 gem \'rails\', \'3.2.10\'

 # Bundle edge Rails instead:
         


        
相关标签:
8条回答
  • 2021-02-02 18:27

    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.

    0 讨论(0)
  • 2021-02-02 18:29

    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
    
    0 讨论(0)
  • 2021-02-02 18:32

    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
    
    0 讨论(0)
  • 2021-02-02 18:33

    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.

    0 讨论(0)
  • 2021-02-02 18:39

    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.

    0 讨论(0)
  • 2021-02-02 18:42

    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
    
    0 讨论(0)
提交回复
热议问题