ajax will_paginate drops the page to first when browser page refresh

530 views Asked by At

I'm beginner in RoR and I have app in which I use ajax with will_paginate like in #174 Pagination with AJAX - RailsCasts. All working fine, but I have issue such as after I select any will_paginate page and then refresh browser page will_paginate drops the page to '1'. I can't think that it is necessary to make that in case of refreshing of the browser page the will_paginate page still remained page selected early. Some my code:

_index_table.html.erb

<%= will_paginate @outdoor_advertisings, :param_name => 'order_page' %>
  <table class="index_table">
    ...
  </table>
<%= will_paginate @outdoor_advertisings, :param_name => 'order_page' %>

index.html.erb

<div id="index_table">
  <%= render 'index_table' %>
</div>

index.js.erb

$("div#index_table").html("<%= escape_javascript(render("index_table")) %>");
$(".ajax-progress").hide();

controller action

class OutdoorAdvertisingsController < ApplicationController
  def index

    if !params[:order_page].present?
      params[:order_page] = '1'
    end

    @outdoor_advertisings = OutdoorAdvertising.where(active: true)
    .paginate(page: params[:order_page], per_page: 30)

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

  end
end

outdoor_advertisings.js.coffee

$("body").on "click", "div#index_table .pagination a", ->
  $(".ajax-progress").show();
  $.getScript @href
  return false

server console log when index page open first time

Started GET "/outdoor_advertisings" for 127.0.0.1 at 2016-12-20 16:36:09 +0200
Processing by OutdoorAdvertisingsController#index as HTML

server console log when select page

Started GET "/outdoor_advertisings?&order_page=3&_=1482244577710" for 127.0.0.1 at 2016-12-20 16:36:20 +0200
Processing by OutdoorAdvertisingsController#index as JS
Parameters: {"order_page"=>"3", "_"=>"1482244577710"}

server console log when browser page refresh after will_paginate page '3' select

Started GET "/outdoor_advertisings" for 127.0.0.1 at 2016-12-20 16:36:24 +0200
Processing by OutdoorAdvertisingsController#index as HTML
1

There are 1 answers

1
Timmy Von Heiss On

Because you are using ajax to dynamically change the content of the DOM, when you refresh the page it starts back at the beginning before the content was dynamically changed. If you were using html and visited page 3 and then hit refresh, it would still be page 3.