当浏览器页面刷新时,ajax will_paginate将页面首先删除

时间:2016-12-20 13:00:55

标签: ruby-on-rails ruby ajax will-paginate

我是RoR的初学者,我有app,其中我使用带有will_paginate的ajax,就像在#174 Pagination with AJAX - RailsCasts中一样。所有工作正常,但我有问题,如我选择任何will_paginate页面,然后刷新浏览器页面will_paginate将页面删除到' 1'。我无法认为有必要在浏览器页面刷新的情况下使will_paginate页面仍然保持早期页面选择。我的一些代码:

_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();

控制器动作

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
索引页面第一次打开时

服务器控制台日志

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

选择页面时的服务器控制台日志

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"}

服务器控制台日志在浏览器页面刷新后,在will_paginate页面&#39; 3&#39;选择

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

1 个答案:

答案 0 :(得分:0)

因为您使用ajax来动态更改DOM的内容,所以当您刷新页面时,它会在内容动态更改之前从头开始。如果您使用的是html并访问过第3页然后点击刷新,它仍然是第3页。