Ruby on rails 将为搜索结果分页吗?
我正在使用will_paginate gem对我的应用程序进行分页。我用以下代码创建了一个搜索操作 控制器Ruby on rails 将为搜索结果分页吗?,ruby-on-rails,ajax,will-paginate,Ruby On Rails,Ajax,Will Paginate,我正在使用will_paginate gem对我的应用程序进行分页。我用以下代码创建了一个搜索操作 控制器 def index @transactions = Transaction.all.paginate(:page => params[:page], :per_page => 10) end def search @transactions = Transaction.paginate(:page => params[:page], :per_page
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>');
它在某种程度上适用于索引操作,ajax方法适用于链接,但当我单击最后一页或第一页链接时,会生成类似于localhost:3000/control?=146374328904&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)
您需要定义以下内容:
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)
很抱歉,这不起作用,因为我已经试过了。在模型中定义self.per_page=10只是一个标识符,所以我们不需要在控制器中设置每页值。但我的问题不是这个。我的问题是url。很抱歉,这不起作用,因为我已经尝试过了。在模型中定义self.per_page=10只是一个标识符,所以我们不需要在控制器中设置每页值。但我的问题不是这个。我认为问题在于url。
@transactions = Transaction.where(:created_at => params[:from_date].to_datetime..(params[:to_date].to_datetime + 1.day)).paginate(:page => params[:page], :per_page => 10)