Ruby-on-rails will_paginate 未定义的方法 `total_pages'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1408852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
will_paginate undefined method `total_pages'
提问by mikewilliamson
What am I missing here? I am using the haml_scaffold generator and the pages work fine with will_paginate. When I start tinkering I end up with this 'total_pages' error and I'm not sure what I am doing that is causing it. Anyone have any insight? I'm using mislav-will_paginate 2.3.11.
我在这里缺少什么?我正在使用 haml_scaffold 生成器,并且页面与 will_paginate 一起工作正常。当我开始修补时,我最终遇到了这个“total_pages”错误,我不确定我在做什么导致它。任何人都有任何见解?我正在使用 mislav-will_paginate 2.3.11。
mike@jauntyHymanalope:~/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
>>
回答by Jirapong
try to change
尝试改变
@locations.paginate(:page => 1, :per_page => 2)
to
到
@locations = @locations.paginate(:page => 1, :per_page => 2)
hope this helps
希望这可以帮助
回答by Neeraj Kumar
include
包括
require 'will_paginate/array'
before loading that code will solve your problem.
在加载该代码将解决您的问题之前。
回答by coletrain
If anyone else is having this problem. In my case I did not call the pagination in my controller method last.
如果其他人有这个问题。就我而言,我最后没有在控制器方法中调用分页。
Example Before:
之前的例子:
@foo = Foo.paginate(:page => params[:page], :per_page => 15)
@foo = Foo.do something
Example After:I called the pagination last within my method since rails reads from top to bottom and it seemed to fix my error.
示例之后:我在我的方法中最后调用了分页,因为 rails 从上到下读取,它似乎修复了我的错误。
@foo = Foo.do something
@foo = Foo.paginate(:page => params[:page], :per_page => 15)
回答by batman
@locations = Location.paginate(:all, :conditions => 'city = springfield')
@locationsmust be an object
@locations必须是一个对象
in your example @locationsis an Array
在你的例子中@locations是一个Array
array cant have total_pages method
数组不能有 total_pages 方法

