ProgramingTip

will_paginate 정의되지 않은 메소드`total_pages '

bestdevel 2020. 11. 29. 12:02
반응형

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
>> 

변화 시키려고

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

...에

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

도움이 되셨기를 바랍니다.


포함

require 'will_paginate/array' 

해당 코드를로드하기 전에 문제가 해결됩니다.


다른 사람이이 문제를 공동으로 사용합니다. 제 경우에는 마지막으로 컨트롤러에서 페이지 매김을 호출하지 않습니다.

이전 예 :

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

이후 예제 : 레일이 위에서 아래로 읽히고 내 오류를 수정하는 것처럼 보였기 때문에 메서드 내에서 페이지 매김을 호출했습니다.

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

@locations = Location.paginate(:all, :conditions => 'city = springfield')

@locations 집집합니다

귀하의 예 @locations에서Array

배열은 total_pages 메소드를 사용합니다.

참고 URL : https://stackoverflow.com/questions/1408852/will-paginate-undefined-method-total-pages

반응형