为什么搜索结果的分页不起作用?

时间:2013-12-07 14:30:15

标签: ruby-on-rails ruby pagination

骑上一个问题: Ruby on rails - pagination on search result

这是我的模特:

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页时,网址显示:

.../search?company=&location_id=&page=2&title=&utf8=%E2%9C%93

,页面显示第1页结果..

怎么了?

2 个答案:

答案 0 :(得分:0)

在模型中:

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

答案 1 :(得分:0)

我认为问题出现在这个地方::page => @page。变量@page来自哪里?当您调用search

Post方法时,您可能应该发送此变量

在模型中:

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