2016-12-20 55 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