简体   繁体   中英

Rails will_paginate persistence

Is there a way to make the results persist across the will_paginate pages? 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.

Here's what I have:

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 

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.

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:

定义范围

Page #2-10 work without an issue:

第2-10页

But the last page always does this, as if will_paginate is adding an extra page for some reason:

最后一页错误

You can pass additional parameters to will_paginate using the :params options.

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)

Added fix to the reports iteration:

          <% 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 %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM