代码之家  ›  专栏  ›  技术社区  ›  mikewilliamson

将“分页未定义的方法”设置为“总页数”

  •  60
  • mikewilliamson  · 技术社区  · 16 年前

    我这里缺什么?我正在使用haml_scaffold生成器,页面可以与will_paginate一起正常工作。当我开始修补的时候,我最终会出现这个“总页数”错误,我不确定我所做的是什么导致了这个错误。有人有什么见解吗? 我正在使用mistrav-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
    >> 
    
    4 回复  |  直到 9 年前
        1
  •  135
  •   Jirapong    16 年前

    尝试改变

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

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

    希望这有帮助

        2
  •  29
  •   Neeraj Kumar    13 年前

    包括

    require 'will_paginate/array' 
    

    在加载之前,该代码将解决您的问题。

        3
  •  10
  •   coletrain    12 年前

    如果其他人有这个问题。在我的例子中,我上次没有调用控制器方法中的分页。

    以前的例子:

    @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)
    
        4
  •  5
  •   Pritesh Jain    13 年前
    @locations = Location.paginate(:all, :conditions => 'city = springfield')
    

    @locations 必须是对象

    在你的例子中 @位置 是一个 Array

    数组不能有total-pages方法