2016-12-20 30 views
0

我是RoR的初学者,我在应用程序中使用ajax和will_paginate,如#174 Pagination with AJAX - RailsCasts。所有工作正常,但我有问题,如我选择任何will_paginate页面,然后刷新浏览器页面will_paginate将页面删除到'1'。我不认为有必要在浏览器页面刷新的情况下,will_paginate页面仍然保持页面选择的早。我的一些代码:ajax 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页面'3'后刷新选择

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

回答

0

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

+0

感谢您的回复。我猜想,在AJAX使用的原因,但我不能发明如何使AJAX继续工作,并没有改变页面的数量在刷新浏览器页面的情况下。你可以提示一个窍门?谢谢。 – MSprut