<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7233.28">
<TITLE>RE: [Rails] Pagination - why is it this hard?</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->
<P><FONT SIZE=2>Pagination can be a little frustrating, but your example doesn't look too bad.<BR>
<BR>
If I understand your question, you're just having problems w/ the links passing the correct parameters?<BR>
<BR>
Just use this:<BR>
<%= link_to "Previous", { :page => @person_pages.current.previous, :letter => params[:letter], :department => params[:department], :q => params[:q] } if<BR>
@person_pages.current.previous -%><BR>
<% pagination_links_each(@person_pages, :window_size => 4) do |n| - %><BR>
<%= link_to n,{:page => n, :letter => params[:letter], :department => params[:department], :q => params[:q]} %><BR>
<% end %><BR>
<%= link_to "Next", { :page => @person_pages.current.next , :letter => params[:letter], :department => params[:department], :q => params[:q] } if<BR>
@person_pages.current.next -%><BR>
<BR>
The "magick" is rails will only supply parameters on the query string if their params has a value (ie, each link won't have &letter=&department=&q=)<BR>
<BR>
Also, your searchcondition can be more easily written as:<BR>
searchconditions = [ "LOWER(username) LIKE :q OR<BR>
LOWER(first_name) LIKE :q OR LOWER(last_name) LIKE :q OR<BR>
LOWER(preferred_name) LIKE :q", { :q => "%#{session[:q]}%" }]<BR>
<BR>
-----Original Message-----<BR>
From: rails-bounces@lists.rubyonrails.org on behalf of Sean Hussey<BR>
Sent: Tue 1/31/2006 10:39 AM<BR>
To: Rails@lists.rubyonrails.org<BR>
Subject: [Rails] Pagination - why is it this hard?<BR>
<BR>
Hi everyone,<BR>
<BR>
I'm at the end of my rope on this. I can't get pagination to work<BR>
with anything but a standard find on a model. If I try to do a search<BR>
and customize the pagination, I get lots of different variations.<BR>
<BR>
My thought was to have the list action do what it does, but to pass it<BR>
a list of search conditions from the search action. So, if search<BR>
determines that we need to get a list of all people in X department,<BR>
it sets up searchconditions and tries to pass it to list. No luck<BR>
there.<BR>
<BR>
My basic question is, what's the best practice way to paginate over<BR>
custom finds? The search results usually come up ok, it's just that<BR>
the pagination links don't pass on the right parameters, so they<BR>
either don't work, or they just go back to the regular list, even<BR>
though the action is search (in the URL).<BR>
<BR>
The most recent attempt has normal listings working correctly, but<BR>
when I perform a search (which displays correctly) and click on any of<BR>
the pagination links, since there's no passing of the search<BR>
variables.<BR>
<BR>
I know how to do this, as I've done it a million times in other<BR>
projects, but I can't find the Rails way to do this that makes me say,<BR>
"Oh, magic!" And, since I'm using rails, I'd rather not go and build<BR>
this the way I would have in Perl.<BR>
<BR>
I'm sorry if I come off as harsh, but I'm incredibly frustrated by<BR>
this. I've been hacking on it for hours now, and I can't launch this<BR>
version of my app without proper pagination. The deadline is today,<BR>
and I figured this would be a lot easier than it seems to be.<BR>
<BR>
Code:<BR>
Controller:<BR>
def list<BR>
@person_pages, @people = paginate :person, :order => 'last_name<BR>
ASC', :per_page => 20<BR>
end<BR>
<BR>
def search<BR>
searchconditions = []<BR>
if params.include?(:letter)<BR>
searchconditions = ["LOWER(last_name) LIKE ?",<BR>
params[:letter].to_str + "%"]<BR>
elsif params.include?(:department)<BR>
searchconditions = ["department_id = ?", params[:department]]<BR>
elsif session[:q]<BR>
if params[:q]<BR>
session[:q] = params[:q].downcase<BR>
searchconditions = [ "LOWER(username) LIKE ? OR<BR>
LOWER(first_name) LIKE ? OR LOWER(last_name) LIKE ? OR<BR>
LOWER(preferred_name) LIKE ?",<BR>
"%" + session[:q] + "%",<BR>
"%" + session[:q] + "%",<BR>
"%" + session[:q] + "%",<BR>
"%" + session[:q] + "%" ]<BR>
end<BR>
else<BR>
session[:q] = ''<BR>
end<BR>
if searchconditions.empty?<BR>
flash[:note] = "You must enter a search phrase to search."<BR>
redirect_to :action => "list"<BR>
else<BR>
@person_pages, @people = paginate :person, :per_page => 20,<BR>
:conditions => searchconditions, :order => 'last_name ASC'<BR>
render :action => "list"<BR>
end<BR>
end<BR>
<BR>
View:<BR>
<%= link_to "Previous", { :page => @person_pages.current.previous } if<BR>
@person_pages.current.previous -%><BR>
<% pagination_links_each(@person_pages, :window_size => 4) do |n| %><BR>
<a href='?page=<%= n %>'><%= n %></a><BR>
<% end %><BR>
<%= link_to "Next", { :page => @person_pages.current.next } if<BR>
@person_pages.current.next -%><BR>
<BR>
Resources I've used to (it seems) no avail:<BR>
<BR>
<A HREF="http://api.rubyonrails.com/classes/ActionController/Pagination.html">http://api.rubyonrails.com/classes/ActionController/Pagination.html</A><BR>
<A HREF="http://wiki.rubyonrails.com/rails/pages/HowtoPagination">http://wiki.rubyonrails.com/rails/pages/HowtoPagination</A><BR>
<A HREF="http://bigbold.com/snippets/posts/show/389">http://bigbold.com/snippets/posts/show/389</A><BR>
<A HREF="http://railsexpress.de/blog/articles/2005/11/04/faster-pagination">http://railsexpress.de/blog/articles/2005/11/04/faster-pagination</A><BR>
_______________________________________________<BR>
Rails mailing list<BR>
Rails@lists.rubyonrails.org<BR>
<A HREF="http://lists.rubyonrails.org/mailman/listinfo/rails">http://lists.rubyonrails.org/mailman/listinfo/rails</A><BR>
<BR>
</FONT>
</P>
</BODY>
</HTML>