Forum: Ruby on Rails help with paginating partials strategy

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
charlie caroff (Guest)
on 2007-06-09 09:12
(Received via mailing list)
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
Morgan Whitney (Guest)
on 2007-06-09 14:51
(Received via mailing list)
On 6/9/07, charlie caroff <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
http://www.blizzo.com
charlie caroff (Guest)
on 2007-06-10 00:14
(Received via mailing list)
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:

<div id="pagination" class="left_pagination"><%=
pagination_links(@recent_search_pages) %></div>

<div id="recent_searches" class="left_column_listing">
  <% for recent_search in @recent_searches %>
  <div id="recent_search_item_<%= recent_search.id %>"
class="box_left" >
    <div class="deleted_news_search_result_title"><%=
link_to(truncate(recent_search.search_term), {:controller =>
'admin', :action => 'research', :id => recent_search.id}) %></div
  </div>
<% end %>
</div>

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
Kevin S. (Guest)
on 2007-06-10 18:04
(Received via mailing list)
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.
charlie caroff (Guest)
on 2007-06-11 22:22
(Received via mailing list)
Thank you.  This is a great tutorial.  I have it working now, with
Ajax.

Charlie
This topic is locked and can not be replied to.