为什么搜索结果的分页不起作用?
骑在前一个问题上: Ruby on rails – 搜索结果的分页
这是我的模特:
def self.search(title, company, location_id) if location_id.present? paginate :conditions => ['title LIKE ? AND company LIKE ? AND location_id = ?', "%#{title}%", "%#{company}%", location_id].last(200), :page => @page, :per_page => 20, :order => "total DESC" else paginate :conditions => ['title LIKE ? AND company LIKE ?', "%#{title}%", "%#{company}%"].last(200), :page => @page, :per_page => 20, :order => "total DESC" end end
当我点击第2页时,url显示:
.../search?company=&location_id=&page=2&title=&utf8=%E2%9C%93
并且页面显示第1页结果..
怎么了?
我认为问题出在这个地方:page => @page
。 变量@page
? 当您调用Post
类的search
方法时,您可能应该发送此变量
在模型中:
def self.search(title, company, location_id, page) if location_id.present? paginate :conditions => ['title LIKE ? AND company LIKE ? AND location_id = ?', "%#{title}%", "%#{company}%", location_id].last(200), :page => page, :per_page => 20, :order => "total DESC" else paginate :conditions => ['title LIKE ? AND company LIKE ?', "%#{title}%", "%#{company}%"].last(200), :page => page, :per_page => 20, :order => "total DESC" end end
在控制器中:
def search ... page = params[:page] @posts = Post.search(title, company, location_id, page); end
在模型中:
def self.search(title, company, location_id, page) ... :page => page, ... end
在控制器中:
def search ... page = params[:page]; @posts = Post.search(title, company, location_id, page); end