Search code examples
ruby-on-railskaminaripaginator

Does Kaminari remote => true wipe out other parameters (like window)?


I am using the kaminari paginator and have it paginating an array fine both with and without ajax. I built the app based off the kaminari example app.

However, I just noticed a problem and am trying to figure out if this is a bug or just misuse on my part.

View:

<%= paginate @pgdata, :remote => true, :window => 2 %>

On initial page load, everything is fine, including the next/previous 2 numbered pages displaying (as per :window => 2). When you click next, previous, first, last or any numbered page, however, it 'forgets' the window setting and reverts to a default of 4. Changing the default is not an option for me as I need to use different window settings in different situations.

This behavior is not present if remote is removed, the window setting is kept properly.

JS:

$('#pglist').html("<%= escape_javascript render 'pglist' %>");
$('#paginator').html('<%= escape_javascript(paginate(@pgdata, :remote => true).to_s) %>');

Controller:

@pgdata = Kaminari.paginate_array(
      @current_user.somequery
      .sort_by(&:created_at)
      .reverse)
  .page(params[:page])
  .per(10)

Rails 4.2.0. Logs are error free.


Solution

  • Figured this out about 5 minutes after posting the question: It was misuse on my part. Since I wasted a silly amount of time hunting for the solution to a very simple and obvious mistake, hopefully this will save someone a little frustration.

    $('#paginator').html('<%= escape_javascript(paginate(@pgdata, :remote => true).to_s) %>');
    

    Needs change to:

    $('#paginator').html('<%= escape_javascript(paginate(@pgdata, :remote => true, :window => 2).to_s) %>');
    

    Since, of course, the JS replaces the <%= paginate ... %> in the view after the first click, you need to add the window setting to the JS paginate as well:

    ", :window => 2"