Rails WillPaginate::Collection not paginating Array
Asked Answered
S

1

1

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

The view;

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

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.

Apparently, the array can only be populated with values of current page. However

   @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

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| %>

something better than

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

that generates undefined methodpage for WillPaginate::Collection`

context: ruby 1.9.3, rails 3.2.17, will_paginate 3.0.5

Spurrier answered 26/5, 2014 at 17:27 Comment(0)
S
1

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

With controller stating:

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.

Spurrier answered 27/5, 2014 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.