簡體   English   中英

Rails will_paginate持久性

[英]Rails will_paginate persistence

有沒有辦法使結果在will_paginate頁面上持久存在? 我使用bootstrap-datepicker-rails列出了給定范圍內的報告,該報告發布了范圍內的結果,但是當我單擊第2頁鏈接時,它恢復為第2頁上沒有范圍的結果。

這是我所擁有的:

views / reports / 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" %> 

controllers / 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頁不是post ,所以我認為這是問題所在。

這不是固定的,每當我定義范圍后單擊最后一頁時,即使仍有記錄要顯示,它總是會返回錯誤。

這是加載時默認頁面的外觀(尚未指定范圍):

默認報告頁

這就是我在第1頁上定義范圍(5/1/15-7/1/15)時的樣子

定義范圍

頁面#2-10正常工作:

第2-10頁

但是最后一頁總是這樣做,好像will_paginate由於某種原因要添加額外的頁面:

最后一頁錯誤

您可以使用: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 %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM