Pergunta

O que estou ausente aqui? Eu estou usando o gerador haml_scaffold ea multa páginas trabalho com will_paginate. Quando eu começar a mexer eu acabar com esse erro 'TOTAL_PAGES' e eu não tenho certeza do que eu estou fazendo que está causando isso. Alguém tem alguma idéia? Estou usando 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
>> 
Foi útil?

Solução

Tente mudar

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

para

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

espero que isso ajude

Outras dicas

incluir

require 'will_paginate/array' 

antes de carregar esse código vai resolver o seu problema.

Se alguém está tendo esse problema. No meu caso eu não chamar a paginação no meu método de controlador passado.

Exemplo Antes:

@foo = Foo.paginate(:page => params[:page], :per_page => 15)
@foo = Foo.do something

Exemplo Depois: Liguei para a paginação última dentro do meu método, pois os trilhos lê de cima para baixo e parecia para corrigir o meu erro.

@foo = Foo.do something
@foo = Foo.paginate(:page => params[:page], :per_page => 15)
@locations = Location.paginate(:all, :conditions => 'city = springfield')

@locations deve ser um objeto

no seu exemplo @locations é um Array

array não pode ter TOTAL_PAGES método

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top