will_paginate undefined 방법`total_pages '
-
05-07-2019 - |
문제
내가 여기서 무엇을 놓치고 있습니까? 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
예제 :Rails가 위에서 아래로 읽히고 내 오류를 고치는 것처럼 보이기 때문에 내 방법 내에서 Pagination을 마지막으로 불렀습니다.
@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 메소드가 없습니다
제휴하지 않습니다 StackOverflow