2009-09-11 113 views
52

我在這裏錯過了什麼?我正在使用haml_scaffold生成器,並且這些頁面在will_paginate下正常工作。當我開始修補時,最終會出現這個'total_pages'錯誤,我不確定我在做什麼導致它。任何人有任何見解? 我正在使用mislav-will_paginate 2.3.11。will_paginate undefined method`total_pages'

[email protected]:~/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 
>> 

回答

130

嘗試改變

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

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

希望這有助於

+1

Ghaaa!我知道這是愚蠢的。一直盯着屏幕。甜蜜的感謝! – mikewilliamson 2009-09-11 11:38:24

+4

謝謝Jirapong!你剛剛解決了我的5小時問題! 爲他人提示:如果使用searchlogic,試圖使用search.paginate會拋出錯誤。在@whatever對象上使用.paginate方法可能會有所幫助:-) – 2009-10-03 18:08:13

+0

我有以下查詢,它與解決方案看起來很相似,但它仍會拋出相同的錯誤:@users = @ company.users.page (params [:page])。sort_by {| u | u.cards.first.card_visits.count} .reverse'任何提示? – 2012-04-25 18:17:26

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

@locations必須是一個對象

在你的例子@locationsArray

陣列斜面具有TOTAL_PAGES方法

+0

will_paginate將此方法賦予數組類,如果您在數組上調用paginate,它會將其轉換爲將paginate集合,其中有哪些方法 – 2011-08-17 12:01:24

28

包括

require 'will_paginate/array' 

加載到代碼將解決問題之前。

+0

在哪裏?在視圖還是控制器? – 2012-04-25 18:15:15

+0

將要求'will_paginate/array'添加到您的控制器。除非你告訴它,否則will_paginate只能用於對象(不在數組上!)。 – 2014-01-04 15:18:47

+3

@Lior - 我已經在控制器中添加了這個以及我嘗試創建一個初始化器,但仍然得到相同的錯誤 – kamal 2015-05-11 09:13:54

7

如果其他人有這個問題。在我的情況中,我最後並沒有在我的控制器方法中調用分頁。

實例之前:

@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) 
+0

我有一個sort_by「分頁」後,這是造成這個問題。謝謝。 – tomascharad 2015-05-12 14:33:55

相關問題