【问题标题】:Array not working with will_paginate, despite requiring 'will_paginate/array'?尽管需要'will_paginate/array',但数组不能与 will_paginate 一起使用?
【发布时间】:2013-03-21 19:25:35
【问题描述】:

我有一个搜索模型,它构造一个基本上是原始 SQL 的字符串。此字符串通过 find_by_sql 调用运行,并在下面的第 5 行返回给控制器(@search.query

1  def roster_builder
2    authorize! :access_roster_builder, current_coach
3    @search = Search.new(sport: current_coach.primary_sport, conditions: params[:search])
4
5    @athletes = @search.query.page(params[:page]).per_page(8)
6
7    if params[:refresh]
8      render "athletes_refresh" 
9    else
10     render "athletes" if request.format.js?
11   end
12 end

这会返回一个数组,但我得到了这个错误:

NoMethodError (undefined method `page' for #<Array:0x00000105b7add0>)

所以我发现在哪里插入以下内容可以解决这个问题:

require 'will_paginate/array'

我已经尝试将它放在我的控制器中,但 will_paginate.rb 初始化文件无济于事。我什至对 find_by_sql 结果做了 .to_a ——同样的错误。这里到底有什么问题??我正在使用 will_paginate 版本 3.0.4,所以它是完全最新的。在 github 上,它甚至说 require 应该可以正常工作:https://github.com/mislav/will_paginate/wiki/Backwards-incompatibility

【问题讨论】:

    标签: sql ruby-on-rails ruby postgresql will-paginate


    【解决方案1】:

    看到这个帖子:https://github.com/mislav/will_paginate/issues/263

    所以对于您的控制器来说,它看起来像这样

    def roster_builder
        require 'will_paginate/array'
      authorize! :access_roster_builder, current_coach
      @search = Search.new(sport: current_coach.primary_sport, conditions: params[:search])
    
      # @athletes = @search.query.page(params[:page]).per_page(8)
        @athletes = @search.query.paginate page: params[:page], per_page: 8
    
      if params[:refresh]
        render "athletes_refresh" 
      else
        render "athletes" if request.format.js?
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2010-12-06
      • 2018-08-24
      • 1970-01-01
      • 2011-02-28
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 2010-11-08
      相关资源
      最近更新 更多