Administrate: Undefined method 'per'

Created on 2 Dec 2015  路  11Comments  路  Source: thoughtbot/administrate

Hi I hit some errors while installing "administrate" but after doing few things i resolved them, and when all seems to be good and i tried to hit "localhost:3000/admin" I got this error.

NoMethodError - undefined method `per' for #Asset::ActiveRecord_Relation:0x007faef5b99820:
Can you please guide me here how can i get away with this.

Server logs are:
Started GET "/admin" for 127.0.0.1 at 2015-11-18 08:10:40 +0500
Processing by Admin::AssetsController#index as HTML
Asset Load (0.3ms) SELECT assets.* FROM assets LIMIT 30 OFFSET 0
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.0ms)

NoMethodError - undefined method per' for #Asset::ActiveRecord_Relation:0x007faef5b99820: activerecord (4.2.5) lib/active_record/relation/delegation.rb:136:inmethod_missing'
activerecord (4.2.5) lib/active_record/relation/delegation.rb:99:in method_missing' administrate (0.1.1) app/controllers/administrate/application_controller.rb:7:inindex'
actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:4:in send_action' actionpack (4.2.5) lib/abstract_controller/base.rb:198:inprocess_action'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:10:in process_action' actionpack (4.2.5) lib/abstract_controller/callbacks.rb:20:inblock in process_action'
activesupport (4.2.5) lib/active_support/callbacks.rb:117:in call' activesupport (4.2.5) lib/active_support/callbacks.rb:555:inblock (2 levels) in compile'
activesupport (4.2.5) lib/active_support/callbacks.rb:505:in call' activesupport (4.2.5) lib/active_support/callbacks.rb:92:inrun_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in _run_process_action_callbacks' activesupport (4.2.5) lib/active_support/callbacks.rb:81:inrun_callbacks'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:19:in process_action' actionpack (4.2.5) lib/action_controller/metal/rescue.rb:29:inprocess_action'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:32:in block in process_action' activesupport (4.2.5) lib/active_support/notifications.rb:164:inblock in instrument'
activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in instrument' activesupport (4.2.5) lib/active_support/notifications.rb:164:ininstrument'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:30:in process_action' actionpack (4.2.5) lib/action_controller/metal/params_wrapper.rb:250:inprocess_action'
activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:18:in process_action' actionpack (4.2.5) lib/abstract_controller/base.rb:137:inprocess'
actionview (4.2.5) lib/action_view/rendering.rb:30:in process' actionpack (4.2.5) lib/action_controller/metal.rb:196:indispatch'
actionpack (4.2.5) lib/action_controller/metal/rack_delegation.rb:13:in dispatch' actionpack (4.2.5) lib/action_controller/metal.rb:237:inblock in action'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:76:in dispatch' actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:45:inserve'
actionpack (4.2.5) lib/action_dispatch/journey/router.rb:43:in block in serve' actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:inserve'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:817:in call' rack (1.6.4) lib/rack/etag.rb:24:incall'
rack (1.6.4) lib/rack/conditionalget.rb:25:in call' rack (1.6.4) lib/rack/head.rb:13:incall'
actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:27:in call' actionpack (4.2.5) lib/action_dispatch/middleware/flash.rb:260:incall'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in context' rack (1.6.4) lib/rack/session/abstract/id.rb:220:incall'
actionpack (4.2.5) lib/action_dispatch/middleware/cookies.rb:560:in call' activerecord (4.2.5) lib/active_record/query_cache.rb:36:incall'
activerecord (4.2.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in call' activerecord (4.2.5) lib/active_record/migration.rb:377:incall'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:29:in block in call' activesupport (4.2.5) lib/active_support/callbacks.rb:88:inrun_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in _run_call_callbacks' activesupport (4.2.5) lib/active_support/callbacks.rb:81:inrun_callbacks'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:27:in call' actionpack (4.2.5) lib/action_dispatch/middleware/reloader.rb:73:incall'
actionpack (4.2.5) lib/action_dispatch/middleware/remote_ip.rb:78:in call' better_errors (2.1.1) lib/better_errors/middleware.rb:84:inprotected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in better_errors_call' better_errors (2.1.1) lib/better_errors/middleware.rb:57:incall'
actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in call' actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:incall'
railties (4.2.5) lib/rails/rack/logger.rb:38:in call_app' railties (4.2.5) lib/rails/rack/logger.rb:20:inblock in call'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in block in tagged' activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:intagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in tagged' railties (4.2.5) lib/rails/rack/logger.rb:20:incall'
actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in call' rack (1.6.4) lib/rack/methodoverride.rb:22:incall'
rack (1.6.4) lib/rack/runtime.rb:18:in call' activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:incall'
rack (1.6.4) lib/rack/lock.rb:17:in call' actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:incall'
rack (1.6.4) lib/rack/sendfile.rb:113:in call' railties (4.2.5) lib/rails/engine.rb:518:incall'
railties (4.2.5) lib/rails/application.rb:165:in call' rack (1.6.4) lib/rack/content_length.rb:15:incall'
thin (1.6.4) lib/thin/connection.rb:86:in block in pre_process' thin (1.6.4) lib/thin/connection.rb:84:inpre_process'
thin (1.6.4) lib/thin/connection.rb:53:in process' thin (1.6.4) lib/thin/connection.rb:39:inreceive_data'
eventmachine (1.0.8) lib/eventmachine.rb:193:in run' thin (1.6.4) lib/thin/backends/base.rb:73:instart'
thin (1.6.4) lib/thin/server.rb:162:in start' rack (1.6.4) lib/rack/handler/thin.rb:19:inrun'
rack (1.6.4) lib/rack/server.rb:286:in start' railties (4.2.5) lib/rails/commands/server.rb:80:instart'
railties (4.2.5) lib/rails/commands/commands_tasks.rb:80:in block in server' railties (4.2.5) lib/rails/commands/commands_tasks.rb:75:inserver'
railties (4.2.5) lib/rails/commands/commands_tasks.rb:39:in run_command!' railties (4.2.5) lib/rails/commands.rb:17:in'
bin/rails:4:in `

Most helpful comment

administrate has kamainari as a dependency.

make an initialiser in app/config/initializers/kaminari.rb

# needed to get will paginate to work with kaminari
Kaminari.configure do |config|
  config.page_method_name = :per_page_kaminari
end

# an initializer to handle conflicting kaminari and will_paginate methods
# config/initializers/will_paginate.rb
if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        def per(value = nil) per_page(value) end
        def total_count() count end
        def first_page?() self == first end
        def last_page?() self == last end
      end
    end
    module CollectionMethods
      alias_method :num_pages, :total_pages
    end
  end
end


and now both will_paginate and kaminari play nice.

All 11 comments

If you have both kaminari and will_paginate this will happen.

No i only have will_paginate.

@johnmeehan @bernerdschaefer @croaky @mjankowski @dctanner please can any of you give me some solution? I am on a tight deadline now.

hi @hamzaiftikhar could you provide your Gemfile.lock in a gist?

administrate has kamainari as a dependency.

make an initialiser in app/config/initializers/kaminari.rb

# needed to get will paginate to work with kaminari
Kaminari.configure do |config|
  config.page_method_name = :per_page_kaminari
end

# an initializer to handle conflicting kaminari and will_paginate methods
# config/initializers/will_paginate.rb
if defined?(WillPaginate)
  module WillPaginate
    module ActiveRecord
      module RelationMethods
        def per(value = nil) per_page(value) end
        def total_count() count end
        def first_page?() self == first end
        def last_page?() self == last end
      end
    end
    module CollectionMethods
      alias_method :num_pages, :total_pages
    end
  end
end


and now both will_paginate and kaminari play nice.

@hamzaiftikhar, sorry I haven't responded sooner.

This issue has come up in #158 - you should be able to find a solution there.

If that doesn't work, feel free to re-open this issue. Cheers!

I have also seen #158 but that didn't work as well. And neither did @johnmeehan's. Can you guys tell me any other way around?

@hamzaiftikhar you might have already tried this, but what happens when you add the following method to app/controllers/admin/application_controller.rb?

  def index
    search_term = params[:search].to_s.strip
    resources = Administrate::Search.new(resource_resolver, search_term).run
    resources = order.apply(resources)
    resources = resources.paginate(:page => params[:page])     
    page = Administrate::Page::Collection.new(dashboard, order: order)

    render locals: {
      resources: resources.paginate(:page => params[:page]),
      search_term: search_term,
      page: page,
    }
  end

Thanks guys it worked finally

I had an issue with my :per_page setting in will_paginate - I had globally WillPaginate.per_page = 6 for the front end of my site, but wanted to show more per page in the admin section. I added to @graysonwright 's code above

    render locals: {
      resources: resources.paginate(:page => params[:page], :per_page => 30),
      search_term: search_term,
      page: page,
    }

and it worked out great. Hope that helps someone

Was this page helpful?
0 / 5 - 0 ratings