有没有办法让结果在will_paginate
页面中保持不变?我在bootstrap-datepicker-rails
的给定范围内列出报告,其中列出了范围的结果,但是当我点击第2页链接时,它会恢复到第2页的结果范围。
以下是我所拥有的:
视图/报告/ 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" %>
&#13;
控制器/ 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
&#13;
这是固定的因此,默认情况下,如果请求不是post
,我会加载所有可用的报告,我认为这是自点击页面以来的问题2不是帖子。
这不是固定每当我在定义一个范围后点击最后一页时,它总会返回一个错误,即使还有剩余的记录要显示。
这里是加载时默认页面的样子(尚未指定范围):
然后我在第1页上定义了一系列(5/1/15 - 7/1/15)时的样子:
Page#2-10没有问题的工作:
但是最后一页总是如此,好像will_paginate因为某些原因添加了额外的页面:
答案 0 :(得分: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_date
或end_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 %>