undefined method page for #<Array:0xc347540> kaminari "page" error. rails_admin
Asked Answered
C

4

1

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?

Clown answered 21/5, 2012 at 15:35 Comment(0)
M
4

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

Muna answered 21/5, 2012 at 16:48 Comment(0)
H
4

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.

Hombre answered 10/12, 2012 at 10:34 Comment(3)
fixed the same issue with mongo and rails_admin - 2 years passed and issue is still thereClank
I get the same error for my array, except it's for current_page rather than page. Will this method still work at all? Also I'm not using Rails_adminLiles
fixed for me using a required mongoid model inside a engine.Sharolynsharon
H
3

Use this

Kaminari.paginate_array(Rule.all).page(params[:page])
Hatshepsut answered 27/9, 2012 at 13:55 Comment(1)
That is the best answer!Phylys
E
0

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)
Enclasp answered 14/3, 2015 at 23:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.