undefined method page for # kaminari “page”

2019-06-07 06:54发布

问题:

i am using rails_admin. when i go to certain resource. by typin url

localhost:3000/admin/rule 

than it give me this error. code is:

scope = Rule.all
scope.page(1).per(2)

. above code is writtten in rails_admin gem.in a file named mongoid.rb placed in adaptors folder. complete log is:

NoMethodError (undefined method `page' for #<Array:0xcea7408>):
mongoid (2.4.8) lib/mongoid/criteria.rb:385:in  `method_missing'
/home/usman/.rvm/gems/ruby-1.9.2-p290@system/bundler/gems/kaminari-809105ad782a/lib/kaminari/models/mongoid_extension.rb:11:in `page'
/home/usman/.rvm/gems/ruby-1.9.2-p290@system/bundler/gems/rails_admin-069819944cc9/lib/rails_admin/adapters/mongoid.rb:37:in `all'
/home/usman/.rvm/gems/ruby-1.9.2-p290@system/bundler/gems/rails_admin-069819944cc9/app/controllers/rails_admin/main_controller.rb:127:in `get_collection'
/home/usman/.rvm/gems/ruby-1.9.2-p290@system/bundler/gems/rails_admin-069819944cc9/app/controllers/rails_admin/main_controller.rb:39:in `list_entries'
/home/usman/.rvm/gems/ruby-1.9.2-p290@system/bundler/gems/rails_admin-069819944cc9/lib/rails_admin/config/actions/index.rb:30:in `block (2 levels) in <class:Index>'

what should i do to resolve this error?

回答1:

You can not call Kaminari methods on Array, because Rule.all will return Array.

So you have to do something like this: Rule.page(1).per(2)

Here is documentation and examples of Kaminari usage:

https://github.com/amatsuda/kaminari



回答2:

I've been running into this issue off and on for a while now using Mongoid. Sometimes refreshing the page in RailsAdmin would fix it.

I figured out that the problem is Kaminari's hooks are not initialized in my environment, so the models that rely on Kaminari's extension methods don't have them available.

I simply took the following line from Kaminari's railtie and put it at the top of my rails_admin initializer:

Kaminari::Hooks.init

Now things seem to be working for me. However, I don't know why the ActiveSupport callback is not running that code.



回答3:

Use this

Kaminari.paginate_array(Rule.all).page(params[:page])


回答4:

Kader's solution is great! The only thing is I found I have to add .per to make it work.

Kaminari.paginate_array(Rule.all).page(params[:page]).per(PER_PAGE_RECORDS)