0

I am using Kaminari with AJAX in my Rails 4 application. I've so far been able to display a different number of records for the first page (13), but then AJAX in 14 records for every page after that. This works more or less perfectly.

However, as soon as I set the number of records for the first page to be a greater number, say 27, kaminari starts loading in duplicates already displayed in that first 27.

In my development application, I only have 30 records total. The first page does indeed show 27, and page 2 should only show 3, however it ends up displaying 14 copies from the first page before showing the last 3 on another page.

Does anyone know why it would be loading in duplicates instead of just showing the last 3 for page 2?

PagesController.rb > index

@dabbles = Dabble.where(:completed_upload => true, :sketch => false).order('created_at DESC')
@dabbles = Kaminari.paginate_array(@dabbles)
@page = (params[:page] || '1').to_i

if @page == 1
   @dabbles = @dabbles.page(params[:page]).per(27)
else
   @dabbles = @dabbles.page(params[:page]).per(14)
end

respond_to do |format|
   format.html 
   format.js  
   format.json 
end

index.html.erb

<div class="grid" id="fresh">
    <%= render :partial => 'pages/hexagons', :locals => { :dabbles => @dabbles, :count => 1 } %>
</div>

<div id="corrections" style="display: inline;">
   <!-- irrelevant. -->
</div>


<div id="paginator">
    <%= link_to_next_page @dabbles, 'Load More', :remote=>true %>
</div>

index.js.erb

$('#fresh').append("<%= escape_javascript render :partial => 'pages/hexagons', :locals => { :dabbles => @dabbles, :count => 0 } %>");
$('#corrections').append("<%= escape_javascript render :partial => 'pages/corrections', :locals => { :correction => correct_helper(@dabbles.size) } %>");
$('#paginator').html("<%= escape_javascript(link_to_next_page @dabbles, 'Load More', :remote=>true) %>");
div
  • 187
  • 5
  • 12

1 Answers1

0

I think Kaminari detect per_page items count with .per method and when you call first time .per(27) you get first 27 elements. And on second page you get 15..28 elements (not 27..41).

Try this way

@paginated_dabbles = Kaminari.paginate_array(@dabbles[27..-1])
@page = (params[:page] || '1').to_i

if @page == 1
   @dabbles[0...27]
else
   @paginated_dabbles = @dabbles.page(params[:page]).per(14)
end
xab3r
  • 51
  • 1
  • 3