是否为搜索结果分页



我在应用程序上使用will_page gem进行分页。我用以下代码创建了一个搜索操作。

控制器

def index
@transactions = Transaction.all.paginate(:page => params[:page], :per_page => 10)
end
def search
@transactions = Transaction.paginate(:page => params[:page], :per_page => 10).where(:created_at => params[:from_date].to_datetime..(params[:to_date].to_datetime + 1.day))
end

search.js.erb

$("#searchResult").replaceWith('<div id = "searchResult"><%=j render 'result' %></div>');

_结果.thml.erb

<div>
<%= @transactions.each do |f| %>
......
......
<% end %>
</div>
<%= will_paginate @transactions %>

index.html.erb

<div id = "searchResult">
<%= render = "result"%>
<% end %>

pagination.js

$(function() {
$(".pagination a").on('click', function(){
$(".pagination").html("Page is loading...");
$.getScript(this.href);
return false;
});
});

它在某种程度上适用于索引操作,ajax方法适用于链接,但当我点击最后一个或第一个页面链接时,会生成类似于localhost:3000/control_=146374342894&page=1此外,每当我进行搜索时,分页链接都会抛出类似的链接localhost:3000/control/search?from_ date=2016-05-18&page=2&to_date=2016-05-20&utf8=%E2%9C%93哪个应该像localhost:3000/control?page=1。有什么解决办法吗。

您需要定义这个:

self.per_page = 10

在您试图对进行分页的模型中

你也可以试试这样的搜索方法:

@transactions = Transaction.where(:created_at => params[:from_date].to_datetime..(params[:to_date].to_datetime + 1.day)).paginate(:page => params[:page], :per_page => 10)

最新更新