What am I missing here? I am using the haml_scaffold generator and the pages work fine with will_paginate. When I start tinkering I end up with this \'total_pages\' error an
@locations = Location.paginate(:all, :conditions => 'city = springfield')
@locations
must be an object
in your example @locations
is an Array
array cant have total_pages method
If anyone else is having this problem. In my case I did not call the pagination in my controller method last.
Example Before:
@foo = Foo.paginate(:page => params[:page], :per_page => 15)
@foo = Foo.do something
Example After: I called the pagination last within my method since rails reads from top to bottom and it seemed to fix my error.
@foo = Foo.do something
@foo = Foo.paginate(:page => params[:page], :per_page => 15)
include
require 'will_paginate/array'
before loading that code will solve your problem.
I was encountering a similar a error undefined method 'total_pages' for ActiveRecord_AssociationRelation
.
It turns out that renaming my variable solved the issue.
Here was my code:
@reviews = @user.reviews.paginate(page: params[:page], per_page: 5).order('created_at DESC')
I changed it to this:
@user_reviews = @user.reviews.paginate(page: params[:page], per_page: 5).order('created_at DESC')
And it solved the issue. It's very bizarre but it there may have been some kind of conflict.
Since I spent a few hours researching, I thought it might help someone one day as well!
try to change
@locations.paginate(:page => 1, :per_page => 2)
to
@locations = @locations.paginate(:page => 1, :per_page => 2)
hope this helps