Patrick Ng Patrick Ng - 1 month ago 7
Ruby Question

Why is pagination on search result not working?

Riding on the previous question:
Ruby on rails - pagination on search result

This is my model:

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


When I click to page 2, the url shows:

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


and the page show page 1 result..

What's wrong?

Answer

I think the problem is in this place: :page => @page . Where is the variable @page from ? You probably should send this variable when you call the search method of Post class

In the model:

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

In the controller:

def search 
  ... 
  page = params[:page]
  @posts = Post.search(title, company, location_id, page);
end
Comments