Ruby on Rails将对数组进行分页 [英] Ruby on Rails will_paginate an array

查看:67
本文介绍了Ruby on Rails将对数组进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以解释如何在对象数组上使用 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将对数组进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆