# Help with paginating partials strategy

**URL:** <https://www.ruby-forum.com/t/help-with-paginating-partials-strategy/106372>\
**Category:** Rails\
**Created:** [June 9, 2007, 7:12am UTC](https://www.ruby-forum.com/t/help-with-paginating-partials-strategy/106372 "2007-06-09T07:12:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie\_caroff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a587f6/32.png) [@charlie\_caroff](https://www.ruby-forum.com/u/charlie_caroff)\
**Post date:** [June 9, 2007, 7:12am UTC](https://www.ruby-forum.com/t/help-with-paginating-partials-strategy/106372/1 "2007-06-09T07:12:09Z")

</div>

Hi, I have a bunch of partials on a page, and I put a simple paginator  
in for each. When I click on page 2 of any pagination link for any  
partial, instead of just rerendering the partial, page 2 for all  
partials is rendered, instead of just page 2 for only one partial.

Any hints?

Charlie

---

<div class="post-metadata">

**Author:** ![charlie\_caroff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a587f6/32.png) [@charlie\_caroff](https://www.ruby-forum.com/u/charlie_caroff)\
**Post date:** [June 9, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/help-with-paginating-partials-strategy/106372/2 "2007-06-09T12:51:39Z")

</div>

On 6/9/07, charlie caroff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, I have a bunch of partials on a page, and I put a simple paginator  
> in for each. When I click on page 2 of any pagination link for any  
> partial, instead of just rerendering the partial, page 2 for all  
> partials is rendered, instead of just page 2 for only one partial.
> 
> Any hints?
> 
> Charlie

This is a tough one with no code to look at, but my guess is that when  
the page is rendered, you use the same paging variable for all the  
partials, so they all change. My suggestion would be to use AJAX.

Create functionality in the controller to render the partials for the  
specific areas, and then generate the paging controls dynamically  
within the same partial, that way the other content is not altered.

You could do this without AJAX as well, by having different paging  
variables for each pagable section.

–  
F. Morgan Whitney

> **[Home - Blizzo](http://www.blizzo.com)**
>
> Insight. Culture. Passion. OUR STORY About Our Clients We recognize that our clients are the key element in our success and that we play an important part in helping build their business and brand. Whether our client is a small service business or a...

---

<div class="post-metadata">

**Author:** ![charlie\_caroff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a587f6/32.png) [@charlie\_caroff](https://www.ruby-forum.com/u/charlie_caroff)\
**Post date:** [June 9, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/help-with-paginating-partials-strategy/106372/3 "2007-06-09T22:14:49Z")

</div>

Hi, I’m a newbie, so please bear with me.

All my partials get their code from one action in my main controller.  
Here is a sample:

def search\_results  
id = params[:id]  
@search\_term = SearchTerm.find(id)  
@listing\_pages, @listings = paginate(:listings,  
:conditions =\> [“search\_term\_id = ? and deleted = ‘0’”, id],  
:order =\> ‘position’,  
:per\_page =\> 50)  
@recent\_search\_pages, @recent\_searches = paginate(:search\_terms,  
:order =\> ‘updated\_at DESC’,  
:per\_page =\> 10)

end

Here’s one of my partials, called \_recent\_searches.rhtml:

\<%= pagination\_links(@recent\_search\_pages) %\>

 \<% for recent\_search in @recent\_searches %\> 

\<%= link\_to(truncate(recent\_search.search\_term), {:controller =\> 'admin', :action =\> 'research', :id =\> recent\_search.id}) %\>\</div 
\<% end %\>

it is rendered in my main view, like so:

render :partial =\> “recent\_searches”

Is my problem that all my partials are rendered in the same action –  
so they draw some pagination method that rails names for me? Should I  
have a separate action for rendering each partial? If so, how do I  
get a partial to find the action I want it to find?

Charlie

---

<div class="post-metadata">

**Author:** ![charlie\_caroff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a587f6/32.png) [@charlie\_caroff](https://www.ruby-forum.com/u/charlie_caroff)\
**Post date:** [June 10, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/help-with-paginating-partials-strategy/106372/4 "2007-06-10T16:04:26Z")

</div>

Pagination is really only intended for one set of pages at a time  
(like pages in a book). The problem with your code is that “?page=2”  
won’t be clear if you want page 2 of the listings or the recent  
searches–I will guess that it will give you both. The built-in  
paginate method uses the value of params[:page] and isn’t customizable  
so that you can use two different params.

You should be able to use custom pagination and paginate both sets of  
results. You’ll just need to send two different params for each  
paginator (let’s say “page” and “rs\_page”). I think that’s the  
easiest way to do it.

I wrote up a guide to pagination that might be helpful in getting you  
started: [http://www.nullislove.com/2007/05/24/pagination-in-rails/](http://www.nullislove.com/2007/05/24/pagination-in-rails/)

If you feel ready to start learning some Ajax, then the previous  
poster’s suggestion is the best way to do it. Create a regular view  
for your listings with simple or custom pagination and then put your  
recent search into a div that Ajax can update when a new page is  
selected. But if that’s intimidating, you can easily start with  
custom pagination and graduate to Ajax later.

HTH,  
Kevin S.

---

<div class="post-metadata">

**Author:** ![charlie\_caroff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a587f6/32.png) [@charlie\_caroff](https://www.ruby-forum.com/u/charlie_caroff)\
**Post date:** [June 11, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/help-with-paginating-partials-strategy/106372/5 "2007-06-11T20:22:07Z")

</div>

Thank you. This is a great tutorial. I have it working now, with  
Ajax.

Charlie
