<!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>
&lt;%= link_to &quot;Previous&quot;, { :page =&gt; @person_pages.current.previous, :letter =&gt; params[:letter], :department =&gt; params[:department], :q =&gt; params[:q] } if<BR>
@person_pages.current.previous -%&gt;<BR>
&lt;% pagination_links_each(@person_pages, :window_size =&gt; 4) do |n| - %&gt;<BR>
&nbsp;&nbsp; &lt;%= link_to n,{:page =&gt; n,&nbsp; :letter =&gt; params[:letter], :department =&gt; params[:department], :q =&gt; params[:q]} %&gt;<BR>
&lt;% end %&gt;<BR>
&lt;%= link_to &quot;Next&quot;, { :page =&gt; @person_pages.current.next ,&nbsp; :letter =&gt; params[:letter], :department =&gt; params[:department], :q =&gt; params[:q] } if<BR>
@person_pages.current.next -%&gt;<BR>
<BR>
The &quot;magick&quot; is rails will only supply parameters on the query string if their params has a value (ie, each link won't have &amp;letter=&amp;department=&amp;q=)<BR>
<BR>
Also, your searchcondition can be more easily written as:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; searchconditions = [ &quot;LOWER(username) LIKE :q OR<BR>
LOWER(first_name) LIKE :q OR LOWER(last_name) LIKE :q OR<BR>
LOWER(preferred_name) LIKE :q&quot;, { :q =&gt; &quot;%#{session[:q]}%&quot; }]<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.&nbsp; I can't get pagination to work<BR>
with anything but a standard find on a model.&nbsp; 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.&nbsp; 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.&nbsp; No luck<BR>
there.<BR>
<BR>
My basic question is, what's the best practice way to paginate over<BR>
custom finds?&nbsp; 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>
&quot;Oh, magic!&quot;&nbsp; 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.&nbsp; I've been hacking on it for hours now, and I can't launch this<BR>
version of my app without proper pagination.&nbsp; 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>
&nbsp; def list<BR>
&nbsp;&nbsp;&nbsp; @person_pages, @people = paginate :person, :order =&gt; 'last_name<BR>
ASC', :per_page =&gt; 20<BR>
&nbsp; end<BR>
<BR>
&nbsp; def search<BR>
&nbsp;&nbsp;&nbsp; searchconditions = []<BR>
&nbsp;&nbsp;&nbsp; if params.include?(:letter)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; searchconditions = [&quot;LOWER(last_name) LIKE ?&quot;,<BR>
params[:letter].to_str + &quot;%&quot;]<BR>
&nbsp;&nbsp;&nbsp; elsif params.include?(:department)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; searchconditions = [&quot;department_id = ?&quot;, params[:department]]<BR>
&nbsp;&nbsp;&nbsp; elsif session[:q]<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if params[:q]<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session[:q] = params[:q].downcase<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; searchconditions = [ &quot;LOWER(username) LIKE ? OR<BR>
LOWER(first_name) LIKE ? OR LOWER(last_name) LIKE ? OR<BR>
LOWER(preferred_name) LIKE ?&quot;,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;%&quot; + session[:q] + &quot;%&quot;,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;%&quot; + session[:q] + &quot;%&quot;,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;%&quot; + session[:q] + &quot;%&quot;,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;%&quot; + session[:q] + &quot;%&quot; ]<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<BR>
&nbsp;&nbsp;&nbsp; else<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; session[:q] = ''<BR>
&nbsp;&nbsp;&nbsp; end<BR>
&nbsp;&nbsp;&nbsp; if searchconditions.empty?<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; flash[:note] = &quot;You must enter a search phrase to search.&quot;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; redirect_to :action =&gt; &quot;list&quot;<BR>
&nbsp;&nbsp;&nbsp; else<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @person_pages, @people = paginate :person, :per_page =&gt; 20,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :conditions =&gt; searchconditions, :order =&gt; 'last_name ASC'<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; render :action =&gt; &quot;list&quot;<BR>
&nbsp;&nbsp;&nbsp; end<BR>
&nbsp; end<BR>
<BR>
View:<BR>
&lt;%= link_to &quot;Previous&quot;, { :page =&gt; @person_pages.current.previous } if<BR>
@person_pages.current.previous -%&gt;<BR>
&lt;% pagination_links_each(@person_pages, :window_size =&gt; 4) do |n| %&gt;<BR>
&nbsp;&nbsp;&nbsp; &lt;a href='?page=&lt;%= n %&gt;'&gt;&lt;%= n %&gt;&lt;/a&gt;<BR>
&lt;% end %&gt;<BR>
&lt;%= link_to &quot;Next&quot;, { :page =&gt; @person_pages.current.next } if<BR>
@person_pages.current.next -%&gt;<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>