有沒有辦法讓結果在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頁的問題是不是一個職位。
這不是固定的每當我在定義範圍後點擊最後一頁時,即使仍有剩餘記錄顯示,它總是返回錯誤。
這裏的默認頁面的外觀加載時一樣(沒有指定範圍還):
那麼這裏是什麼樣子時,我定義一系列的像(15年5月1日 - 7/1/15)第1頁:
頁#2-10工作沒有問題:
但最後一頁總是這樣做,彷彿是will_paginate添加額外的頁面由於某種原因:
你的過濾提交的'POST'不存在。您可以使過濾器表單在'GET'中提交,並將它們保存在will_paginate鏈接中。您可以自定義'LinkRenderer'來保存查詢參數。 – HungryCoder
有沒有這樣做的例子嗎?它是一個'will_paginate'覆蓋,或整個應用程序? – Godzilla74