undefined method page for #<Array:0xc347540> kaminari “page” error. rails_admin

半世苍凉 提交于 2019-12-08 03:45:27

问题


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)


来源:https://stackoverflow.com/questions/10688256/undefined-method-page-for-array0xc347540-kaminari-page-error-rails-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!