web-dev-qa-db-ja.com

will_paginate未定義メソッド「total_pages」

ここで何が欠けていますか?私はhaml_scaffoldジェネレーターを使用しており、ページはwill_paginateで正常に動作します。私がいじくり始めたとき、私はこの「total_pages」エラーで終わります。誰にも洞察がありますか?私は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
>> 
61
mikewilliamson

変えようとする

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

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

お役に立てれば

135
Jirapong

含める

require 'will_paginate/array' 

そのコードをロードする前に問題を解決します。

29
Neeraj Kumar

他の誰かがこの問題を抱えている場合。私の場合、最後にコントローラーメソッドのページネーションを呼び出しませんでした。

前の例:

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

後の例: Railsは上から下に読み取り、エラーを修正したようです。

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

@locationsはオブジェクトでなければなりません

あなたの例では@locationsArrayです

配列にはtotal_pagesメソッドがありません

5
batman