Ruby on Rails will_paginate 一个数组 [英] Ruby on Rails will_paginate an array
问题描述
我想知道是否有人可以解释如何在对象数组上使用 will_paginate?
I was wondering if someone could explain how to use will_paginate on an array of objects?
例如,在我的网站上,我有一个意见部分,用户可以在其中对意见进行评分.这是我编写的一种方法,用于收集对意见进行评分的用户:
For example, on my site I have an opinion section where users can rate the opinions. Here's a method I wrote to gather the users who have rated the opinion:
def agree_list
list = OpinionRating.find_all_by_opinion_id(params[:id])
@agree_list = []
list.each do |r|
user = Profile.find(r.profile_id)
@agree_list << user
end
end
谢谢
推荐答案
will_paginate 3.0 旨在采用Rails 3 中新的 ActiveRecord::Relation
的优势,因此它默认仅在关系上定义 paginate
.它仍然可以处理数组,但您必须告诉 rails 需要该部分.
will_paginate 3.0 is designed to take advantage of the new ActiveRecord::Relation
in Rails 3, so it defines paginate
only on relations by default. It can still work with an array, but you have to tell rails to require that part.
在你的 config/initializers
文件中(我使用了 will_paginate_array_fix.rb
),添加这个
In a file in your config/initializers
(I used will_paginate_array_fix.rb
), add this
require 'will_paginate/array'
然后你可以在数组上使用
Then you can use on arrays
my_array.paginate(:page => x, :per_page => y)
这篇关于Ruby on Rails will_paginate 一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!