2015-10-13 129 views
0

有沒有辦法讓結果在will_paginate頁面上持續存在?我在bootstrap-datepicker-rails中列出了一個給定範圍的報告,其中發佈了一個範圍的結果,但是當我點擊頁面2鏈接時,它會回到沒有範圍的第2頁上的結果。Rails will_paginate persistence

這是我有:

的意見/報告/ index.html.erb

<%= render partial: "pages/navigation" %> 
 

 
    <div class="row reports"> 
 
     <div class="col-md-2 sidebar no-float"> 
 
     <h3 class="text-center">Select Date Range</h3> 
 
     <div class="input-daterange input-group" id="datepicker"> 
 
      <%= form_tag reports_index_path, method: :post, :class => "form-inline" do %> 
 
      <div class="form-group"> 
 
      <%= label_tag "Start Date:" %> 
 
      <%= text_field_tag(:start_date, @reports.blank? ? '' : params[:start_date], class: "form-control", name: "start_date", data: {"behaviour" => "datepicker"}) %> 
 
      <p class="text-center">to</p> 
 
      <%= label_tag "End Date:" %> 
 
      <%= text_field_tag(:end_date, @reports.blank? ? '' : params[:end_date], class: "form-control", name: "end_date", data: {"behaviour" => "datepicker"}) %> 
 
      </div> 
 
      <br/> 
 
      <br/> 
 
      <%= submit_tag "Update", class: "update-button" %> 
 
      <% end %> 
 
     </div> 
 
     </div> 
 

 

 
     <div class="col-md-10 report-list no-float"> 
 
     <h2 class="text-center">Reports Archive</h2> 
 
     <% @reports.in_groups_of(3).each do |group| %> 
 
      <ul id="report-list list"> 
 
       <% group.each do |report| %> 
 
       <div class="col-md-4"> 
 
        <li class="report"> 
 
        <div class="report-header"> 
 
         <p> 
 
         <span class="report-data"><%= report.name %></span> 
 
         </p> 
 
        </div> 
 
        <p class="report-text"><%= report.range %></p> 
 
        <p class="report-text"><%= link_to "Download", report.pdf.url, target: "_blank" %></p> 
 
        </li> 
 
       </div> 
 
       <% end %> 
 
      </ul> 
 
     <% end %> 
 
     </div> 
 
    </div> 
 
<!-- </div> --> 
 

 
<div class="pagination-wrapper"> 
 
    <%= will_paginate @reports, :previous_label => "Newer", :next_label => "Older", :params => { :start_date => params[:start_date], :end_date => params[:end_date] } %> 
 
</div> 
 

 

 
<%= render partial: "pages/footer" %>

控制器/ reports_controller.rb

class ReportsController < ApplicationController 
 

 
    def index 
 
    @company = current_user.company 
 

 
    @locations = if @company 
 
     current_user.company_locations.order(:name) 
 
    else 
 
     [] 
 
    end 
 

 
    unless @company.nil? || @company.reports.empty? 
 
     if request.post? 
 
     @reports = @company.reports.where(created_at: report_params[:start_date]..report_params[:end_date]).order(created_at: :asc).paginate(page: params[:page], :per_page => 30) 
 
     else 
 
     if params[:start_date].present? && params[:end_date].present? 
 
      @reports = @company.reports.where(created_at: (report_params[:start_date]..report_params[:end_date])).order(created_at: :desc).paginate(page: params[:page], :per_page => 30) 
 
     else 
 
      @reports = @company.reports.order(created_at: :desc).paginate(page: params[:page], :per_page => 30) 
 
     end 
 
     end 
 
    end 
 
    end 
 

 
    private 
 

 
    def report_params 
 
     params.permit(:start_date, :end_date) 
 
    end 
 

 
end

這是固定所以,在默認情況下我加載所有可用報告如果請求不是post,我以爲是因爲點擊到第2頁的問題是不是一個職位。

這不是固定的每當我在定義範圍後點擊最後一頁時,即使仍有剩餘記錄顯示,它總是返回錯誤。

這裏的默認頁面的外觀加載時一樣(沒有指定範圍還):

Default Report Page

那麼這裏是什麼樣子時,我定義一系列的像(15年5月1日 - 7/1/15)第1頁:

Defined Range

頁#2-10工作沒有問題:

Pages 2-10

但最後一頁總是這樣做,彷彿是will_paginate添加額外的頁面由於某種原因:

Error on last page

+0

你的過濾提交的'POST'不存在。您可以使過濾器表單在'GET'中提交,並將它們保存在will_paginate鏈接中。您可以自定義'LinkRenderer'來保存查詢參數。 – HungryCoder

+0

有沒有這樣做的例子嗎?它是一個'will_paginate'覆蓋,或整個應用程序? – Godzilla74

回答

1

您可以通過其他參數使用:params選項will_paginate。

你的具體情況,你可以做到以下幾點:

<div class="pagination-wrapper"> 
    <%= will_paginate @reports, :previous_label => "Newest", :next_label => "Oldest", :params => { :start_date => @start_date, :end_date => @end_date } %> 
</div> 

和修改控制器是這樣的:

unless @company.nil? || @company.reports.empty? 
    if request.post? 
    @reports = @company.reports.where(created_at: report_params[:start_date]..report_params[:end_date]).order(created_at: :asc).paginate(page: params[:page], :per_page => 30) 
    @start_date, @end_date = @reports.last.created_at, @reports.first.created_at 
    else 
    if params[:start_date].present? && params[:end_date].present? 
     @reports = @company.reports.where(created_at: (params[:start_date]..params[:end_date])).order(created_at: :desc).paginate(page: params[:page], :per_page => 30) 
     @start_date, @end_date = params[:start_date], params[:end_date] 
    else 
     @reports = @company.reports.order(created_at: :desc).paginate(page: params[:page], :per_page => 30) 
     @start_date, @end_date = @reports.first.created_at, @reports.last.created_at 
    end 
    end 
end 

注:沒有把握的start_dateend_date其中,先於其他,你可能想要確保範圍是正確定義的,即(最老的..最近)

新增固定在reports迭代:當你點擊一個鏈接分頁

  <% group.each do |report| %> 
      <% unless report.nil? %> 
      <div class="col-md-4"> 
       <li class="report"> 
       <div class="report-header"> 
        <p> 
        <span class="report-data"><%= report.name %></span> 
        </p> 
       </div> 
       <p class="report-text"><%= report.range %></p> 
       <p class="report-text"><%= link_to "Download", report.pdf.url, target: "_blank" %></p> 
       </li> 
      </div> 
      <% end %> 
      <% end %> 
+0

start_date是較早的日期,end_date是較新的。我喜歡這個建議,因爲它可以讓它更容易,但是現在在完成你的建議之後,它會在will_paginate行上拋出一個錯誤「未定義的方法'created_at'for nil:NilClass'。 – Godzilla74

+0

這意味着無論是「@ reports.first」還是「@ reports.last」都是「nil」,這意味着「@ reports」本身是「無」(或空)。這有可能嗎? – Jaxx

+0

我剛擴大範圍,其中應包括60多個報告,其中15個到一個頁面,其他頁面上應該有結果。 – Godzilla74

相關問題