will_paginate undefined method `total_pages' for []:Array

527 views
Skip to first unread message

Philipp Kursawe

unread,
Jan 17, 2012, 6:00:12 PM1/17/12
to pad...@googlegroups.com
I followed the instructions in the install guide on the will_paginate
wiki but still get this error, when I want to use the view helper in
padrino.

My controller fetches the collection
@slots = @license.slots.paginate(:page => params[:page]).reject {|s|
!s.visible }

and my .slim view does a:
==will_paginate @slots

I am getting:

undefined method `total_pages' for []:Array

What's wrong here?

adam moore

unread,
Jan 17, 2012, 8:05:19 PM1/17/12
to pad...@googlegroups.com
Can you confirm which version of the Gem you are using, and that you
are manually adding the sinatra helper?
Also, which ORM are you using?

## app.rb
helpers WillPaginate::Sinatra::Helpers

## boot.rb
Padrino.before_load do
require 'will_paginate/data_mapper' # or active_record/sequel
DataMapper.finalize
end

If it turns out you can't get around it, try adding:

require 'will_paginate/array'

Sorry, I'm not in a situation where I can test a better solution, but
that should allow you to paginate an array.

Adam

--
----=^.^=---

Philipp Kursawe

unread,
Jan 18, 2012, 2:41:07 AM1/18/12
to pad...@googlegroups.com
Hello Adam,

> Can you confirm which version of the Gem you are using, and that you
> are manually adding the sinatra helper?

I am using 3.0.2 of the gem and this is my app.rb

class Admin < Padrino::Application
register Padrino::Mailer
register Padrino::Helpers
register Padrino::Rendering
register Padrino::Admin::AccessControl

helpers WillPaginate::Sinatra::Helpers
...
end

> Also, which ORM are you using?

I am using DataMapper.

> ## boot.rb
> Padrino.before_load do
>  require 'will_paginate/data_mapper' # or active_record/sequel
>  DataMapper.finalize
> end

mine looks like this, cause I specified required=>nil in the Gemfile.
But it makes no difference, even when bundler autoloads it.

Padrino.before_load do
require 'will_paginate'
require 'will_paginate/data_mapper'
end

I am not calling DataMapper.finalize, is it required? I am using
migrations instead of auto_upgrade.

> If it turns out you can't get around it, try adding:
>
> require 'will_paginate/array'

I added that in my Padrino_load to no avail.

>
> Sorry, I'm not in a situation where I can test a better solution, but
> that should allow you to paginate an array.

I hope I can find a solution.

It works in the irb though:

irb(main):068:0> require "will_paginate/array"
=> true
irb(main):069:0> [].paginate
=> []
irb(main):070:0> [1,2,3,4,5].paginate
=> [1, 2, 3, 4, 5]
irb(main):071:0> [1,2,3,4,5].paginate(:per_page=>2)
=> [1, 2]
irb(main):072:0> [1,2,3,4,5].paginate(:per_page=>2).
Display all 4407 possibilities? (y or n)
irb(main):072:0> a = [1,2,3,4,5].paginate(:per_page=>2)
=> [1, 2]
irb(main):073:0> a.total_pages
=> 3

Phil

adam moore

unread,
Jan 18, 2012, 5:30:43 AM1/18/12
to pad...@googlegroups.com


Try installing pry and calling it just before your view render and having a poke around..

Does it throw the same error in irb when you try it on @license.slots?
--
----=^.^=---

Philipp Kursawe

unread,
Jan 20, 2012, 11:29:57 AM1/20/12
to pad...@googlegroups.com
I have had difficulties with pry on Windows, but I will try again.
Reply all
Reply to author
Forward
0 new messages