Tyl*_*ler 1 arrays will-paginate ruby-on-rails-3
我已经添加
# config/initializers/will_paginate_array_fix.rb
require 'will_paginate/array'
Run Code Online (Sandbox Code Playgroud)
但似乎还没有获得对数组的分页支持,例如:
def index
@array = (1..100).to_a.paginate(params[:page])
end
# gives TypeError: can't convert Symbol into Integer
Run Code Online (Sandbox Code Playgroud)
它适用于模型,我得到
defined? WillPaginate # => constant
ActiveRecord::Base.respond_to? :paginate # => true
# but:
Array.respond_to? :paginate # => false
Run Code Online (Sandbox Code Playgroud)
任何人都知道我缺少什么来获得数组的分页支持?
通过查看will_paginate/array中的源代码找到答案:
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
Run Code Online (Sandbox Code Playgroud)
因此对于数组,您必须使用.paginate(而不是.page),并且必须将其作为哈希传递.以下是有效的:
def index
@array = (1..100).to_a.paginate(page: params[:page])
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2723 次 |
| 最近记录: |