will_paginate undefined method `total_pages'
Asked Answered
O

5

67

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 and I'm not sure what I am doing that is causing it. Anyone have any insight? I'm using mislav-will_paginate 2.3.11.

mike@jauntyjackalope:~/project$ script/console
Loading development environment (Rails 2.3.4)
>> @locations = Location.find_all_by_city("springfield")
=> [#<Location id: 3, address: "123 Main St", city: "springfield", phone: "321-1234", business_id: 2>]
>> @locations.class
=> Array
>> @locations.collect{|loc| loc.business}
=> [#<Business id: 2, name: "A Bar", website: "www.abar.com", business_owner_id: 1, created_at: "2009-08-31 21:13:10", updated_at: "2009-08-31 21:13:10">]
>> @locations.class
=> Array
>> @locations.paginate(:page => 1, :per_page => 2)
=> [#<Location id: 3, address: "123 Main St", city: "springfield", phone: "321-1234", business_id: 2>]
>> helper.will_paginate(@locations)
NoMethodError: undefined method `total_pages' for #<Array:0xb6f83bc4>
 from /var/lib/gems/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/view_helpers.rb:197:in `total_pages_for_collection'
 from /var/lib/gems/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/view_helpers.rb:98:in `will_paginate'
 from (irb):7

>> y @locations
--- 
- !ruby/object:Location 
  attributes: 
    city: springfield
    business_id: "2"
    id: "3"
    phone: 321-1234
    address: 123 Main St
  attributes_cache: {}

  business: !ruby/object:Business 
    attributes: 
      name: A Bar
      updated_at: 2009-08-31 21:13:10
      website: www.abar.com
      id: "2"
      created_at: 2009-08-31 21:13:10
      business_owner_id: "1"
    attributes_cache: {}

=> nil
>> 
Obie answered 11/9, 2009 at 3:34 Comment(0)
P
145

Try to change

@locations.paginate(:page => 1, :per_page => 2)

to

@locations = @locations.paginate(:page => 1, :per_page => 2)
Pushed answered 11/9, 2009 at 6:11 Comment(5)
Ghaaa! I knew it was something dumb like that. Been staring at the screen to long. Sweet thanks!Obie
Thanks Jirapong! You just solved my 5 hours problem! Hint for others: If using searchlogic, trying to use search.paginate will throw errors. Using the .paginate method on the @whatever object you are actually searching for might help :-)Cychosz
I have the following query, which looks similar to the solutions but it still throws the same error @users = @company.users.page(params[:page]).sort_by {|u| u.cards.first.card_visits.count }.reverse any tips?Belgrade
switching the method from page to paginate gives me can't dup NilClass error on the same query.Belgrade
paginate() a new array with paginatable but it didn't make @locations paginatable array.Pushed
B
29

include

require 'will_paginate/array' 

before loading that code will solve your problem.

Bringingup answered 3/11, 2011 at 11:46 Comment(3)
where? in the view or the controller?Belgrade
Add require 'will_paginate/array' to your controller. will_paginate works only on objects (not on array!) unless you're telling it to.Lohner
@Lior - i have added this in controller as well as i tries with creating an initializer, but still getting the same errorMarsha
P
12

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)
Pascia answered 21/7, 2013 at 18:32 Comment(1)
I had a sort_by after "paginate" and this was causing this problem. Thanks.Songsongbird
A
5
@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

Almond answered 23/11, 2010 at 13:25 Comment(1)
will_paginate gives this method to the array class, if you call paginate on an array, it will convert it to will paginate collection, which have this methodPrecursor
K
1

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!

Kiushu answered 19/7, 2020 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.