Rails WillPaginate::Collection 不分页数组 [英] Rails WillPaginate::Collection not paginating Array

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

问题描述

正在生成现有数组 vparray,然后按非数据库列 rate 排序.然后需要对其进行分页:

An existing array vparray is being generated, then sorted by a non-db column rate. It then needs to be paginated :

    @vps  = vparray.sort_by{|e| e.rate}
    @vps = WillPaginate::Collection.create(1, 10, @vps.length) do |pager|
      pager.replace @vps
    end

视图;

<%= will_paginate @vps, :previous_label => "prev ", :next_label => " next" -%>

渲染得很好,页面数显示出来,页面显然是第一个.但是,基于: <% @vps.each do |product|%>整个 排序数组正在渲染.

renders fine, the number of pages pans out and the page is apparently the first. However, upon: <% @vps.each do |product| %>, the entire sorted array is being rendered.

显然,数组可以仅填充当前页面的值.不过

   @vps  = vparray.sort_by{|e| e.rate}
   @vps = @vps.paginate(:page => params[:page], :per_page => 10)
   @vps = WillPaginate::Collection.create(1, 10, @vps.length) do |pager|
      pager.replace @vps
    end

不正确.paginate 命令实际上将搜索结果减少到与 per_page 相同的数量,因此 == 仅 1 页!

is incorrect. The paginate command actually reduces the found set to the same number as per_page and therefore == only 1 page!

所以我假设那条线不应该在那里.视图应该调用正确的结果页面

So I'm assuming that line should not be there. The view should be calling the proper page of results

<% @vps.each do |product| %>

比什么都好

<% @vps.page(params[:page]).each do |product| %>

为 WillPaginate::Collection` 生成未定义方法页面

that generates undefined methodpage for WillPaginate::Collection`

上下文:红宝石 1.9.3,导轨 3.2.17,will_paginate 3.0.5

context: ruby 1.9.3, rails 3.2.17, will_paginate 3.0.5

推荐答案

去重读 collection.rb 和 array.rb 库.

Went and re-read the collection.rb and array.rb libraries.

控制器声明:

require "will_paginate/array"

@vgps  = vgp.sort_by{|e| e.rate}
@vgps = @vgps.paginate(:page => params[:page], :per_page => 30)

这是一个有序数组所需的全部内容.

This is all that is necessary for a sorted array.

这篇关于Rails WillPaginate::Collection 不分页数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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