Help with paginating partials strategy

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

On 6/9/07, charlie caroff [email protected] 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

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

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/

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.

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

Charlie