2012-06-29 22 views
1

我添加will_paginate不使用數組

# config/initializers/will_paginate_array_fix.rb 
require 'will_paginate/array' 

但仍然工作似乎並沒有得到好數組分頁支持,如:

def index 
    @array = (1..100).to_a.paginate(params[:page]) 
end 
# gives TypeError: can't convert Symbol into Integer 

它正常工作與模型,我也得到

defined? WillPaginate # => constant 
ActiveRecord::Base.respond_to? :paginate # => true 
# but: 
Array.respond_to? :paginate # => false 

任何人都知道我缺少數組的分頁支持嗎?

回答

5

在will_paginate /陣列看源代碼找到了答案:

def paginate(options = {}) 
    page  = options[:page] || 1 
    per_page = options[:per_page] || WillPaginate.per_page 
    total = options[:total_entries] || self.length 

    WillPaginate::Collection.create(page, per_page, total) do |pager| 
    pager.replace self[pager.offset, pager.per_page].to_a 
    end 
end 

因此對於陣列,則必須使用.paginate(未。第),並且必須將其傳遞作爲散列。所以下面的工作:

def index 
    @array = (1..100).to_a.paginate(page: params[:page]) 
end 
+1

這似乎不工作了。 – Jeff