简体   繁体   English

Rails will_paginate持久性

[英]Rails will_paginate persistence

Is there a way to make the results persist across the will_paginate pages? 有没有办法使结果在will_paginate页面上持久存在? I'm listing reports through a given range with bootstrap-datepicker-rails , which posts the results for a range, but then when I click on the page 2 link, it reverts back to what the results would be on page 2 without the range. 我使用bootstrap-datepicker-rails列出了给定范围内的报告,该报告发布了范围内的结果,但是当我单击第2页链接时,它恢复为第2页上没有范围的结果。

Here's what I have: 这是我所拥有的:

views/reports/index.html.erb 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 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 

This is fixed So, by default I'm loading all available reports if the request isn't a post , which I assume is the issue since clicking to page 2 isn't a post. 这是固定的,因此,默认情况下,如果请求不是post ,则我将加载所有可用报告,因为单击第2页不是post ,所以我认为这是问题所在。

This isn't fixed Whenever I click on the last page after defining a range it always returns an error, even though there are still records left to show. 这不是固定的,每当我定义范围后单击最后一页时,即使仍有记录要显示,它总是会返回错误。

Here's what the default page looks like when loaded (without range specified yet): 这是加载时默认页面的外观(尚未指定范围):

默认报告页

Then here's what it looks like when I define a range of (5/1/15 - 7/1/15) on Page 1: 这就是我在第1页上定义范围(5/1/15-7/1/15)时的样子

定义范围

Page #2-10 work without an issue: 页面#2-10正常工作:

第2-10页

But the last page always does this, as if will_paginate is adding an extra page for some reason: 但是最后一页总是这样做,好像will_paginate由于某种原因要添加额外的页面:

最后一页错误

You can pass additional parameters to will_paginate using the :params options. 您可以使用:params选项将其他参数传递给will_paginate。

In your particular case, you could do the following: 在您的特定情况下,您可以执行以下操作:

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

and amend the controller like this: 并像这样修改控制器:

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

Note: not sure which of start_date or end_date is anterior to the other, you probably want to make sure the range is defined correctly, ie (oldest..most recent) 注意:不确定start_dateend_date哪个先于另一个,您可能要确保正确定义了范围,即(最早(..最新))。

Added fix to the reports iteration: 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