Rails 3: undefined method `page' for #<Array:0xafd0660>
Asked Answered
R

5

29

I can't get past this. I know I've read there isn't a page method for arrays but what do I do?

If I run Class.all in the console, it returns #, but if I run Class.all.page(1), I get the above error.

Any ideas?

Riccio answered 3/8, 2011 at 23:44 Comment(1)
Note: Model.all in Rails 3 returns an array of records causing this issue. Model.all in Rails 4+ returns an ActiveRecord::Relation which you can indeed chain kaminari scopes, page and per, to. Rails Release notesApocryphal
I
44

No Array doesn't have a page method.

Looks like you are using kaminari. Class.all returns an array, thus you cannot call page on it. Instead, use Class.page(1) directly.

For normal arrays, kaminari has a great helper method:

Kaminari.paginate_array([1, 2, 3]).page(2).per(1)
Incumbency answered 4/8, 2011 at 1:24 Comment(0)
A
12

Kaminari now has a method for paginating arrays, so you can do something like this in your controller:

myarray = Class.all
@results = Kaminari.paginate_array(myarray).page(params[:page])
Avera answered 19/2, 2012 at 11:48 Comment(2)
I get an undefined method "paginate_array" for Kaminari:module how to solve this?Coppice
@Þaw Sounds like you were using an older version of Kaminari.Avera
B
5

When you get an undefined method page for Array, probably you are using kaminari gem and you are trying to paginate your Model inside a controller action.

NoMethodError at /
undefined method `page' for # Array

There you need to remind yourself of two things, that the collection you are willing to paginate could be an Array or an ActiveRecordRelation or of course something else.

To see the difference, lets say our model is Product and we are inside our index action on products_controller.rb. We can construct our @products with lets say one of the following:

@products = Product.all

or

@products = Product.where(title: 'title')

or something else... etc

Either ways we get your @products, however the class is different.

@products = Product.all
@products.class
=> Array

and

@products = Product.where(title: 'title')
@products.class
=> Product::ActiveRecordRelation

Therefore depending on the class of the collection we are willing to paginate Kaminari offers:

@products = Product.where(title: 'title').page(page).per(per)
@products = Kaminari.paginate_array(Product.all).page(page).per(per)

To summarise it a bit, a good way to add pagination to your model:

def index
  page = params[:page] || 1
  per  = params[:per]  || Product::PAGINATION_OPTIONS.first
  @products = Product.paginate_array(Product.all).page(page).per(per)

  respond_to do |format|
    format.html
  end

end

and inside the model you want to paginate(product.rb):

paginates_per 5
# Constants
PAGINATION_OPTIONS = [5, 10, 15, 20]
Bonnice answered 25/8, 2014 at 12:36 Comment(0)
P
1

I fixed the issue by invoking Kaminari's hooks manually. Add this line to run in one of your first initializers:

Kaminari::Hooks.init

I posted more details in another answer:

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

Prandial answered 10/12, 2012 at 10:36 Comment(0)
G
0

I had the same error. Did bundle update then restarted the server. One of the two fixed it.

Gosling answered 16/2, 2012 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.