Will_paginate question

Hi-

Is it possible to use search parameters with will_paginate?

For example, something like this:

@var = Model.paginate :per_page => 20, :page => params[:page],
:order => ‘my_date DESC’, :conditions
=>[“my_date >= ? AND my_date <=?”,@start,@end]

I am getting some errors with this, but will this generally work?

If you don’t get a suitable answer here, you should know that there is
a will_paginate Google Group, just like this rubyonrails-talk group.
Naturally, it’s called “will_paginate”. :slight_smile:

n

I haven’t used the substitution like you have, but I can do it with a
conditions hash:

@items = Item.paginate(:all,
:order => “featured DESC, #{@sort.sort}, id DESC”,
:conditions => @filters,
:page => params[:page])

where @filters is a hash like {:hidden => false, :category_id => 5}.
I notice that you don’t have an :all or :first at the beginning.
Maybe this matters.

-Kyle

Hi Pete,

Yes you can do conditional paginates. See my search method below.

Note that you have to remember the search conditions between pages
because you have to restate them on calls for subsequent pages. I stick
mine in the session.

Hope this helps,
John

def full_search
unless params[:page]
session[:search] = [“category_id = ? and address_posttown like ?
and address_postcode like ? and published is true”,
params[:item][:category_id],
(params[:item][:address_posttown].to_s) +"%",
(params[:item][:address_postcode].to_s) +"%"]
end

@items = Item.paginate :page => params[:page],
                       :conditions => session[:search]

if @items.empty?
  flash.now[:error] = "Sorry, nothing matched your search request"
end

# old-fashioned reload for browsers not doing AJAX
render :action => :search unless request.xhr?

end