Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is how I set up things:</p> <p>Let assume I have an <code>Item</code> model.</p> <p>First in the index view, I render a partial where pagination is performed:</p> <pre><code>&lt;div id="list"&gt; &lt;%= render 'items/paginate_list', :items =&gt; @items %&gt; &lt;/div&gt; </code></pre> <p>In the partial, I add a css class to my <code>will_paginate</code> statement:</p> <pre><code>items/_paginate_list.html.erb &lt;%= will_paginate items, :param_name =&gt; :page, :class =&gt; 'my_pagination' %&gt; # code displaying each item... </code></pre> <p>Then in the application.js, I catched events of clicking on the links of <code>my_pagination</code> class:</p> <pre><code>$(document).ready(function(){ $('.my_pagination a').live("click",function(){ $('.my_pagination').html('Page is loading, please wait...'); $.get(this.href, null, null, 'script'); return false; }); }); </code></pre> <p>This way the call is performed to the controller in ajax through the <code>$.get</code> statement. The pagination bar displays a message that page is loading.</p> <p>In the controller, I just have the usual:</p> <pre><code>class ItemsController &lt; ApplicationController def index @items=Item.paginate(:page =&gt; params[:page], :per_page =&gt; 10) end end </code></pre> <p>Finally, in the embedded js, I feed the partial with pagination to be reloaded:</p> <pre><code>items/index.js.erb $("#list").html("&lt;%= escape_javascript(render 'items/paginate_list', :items =&gt; @items) %&gt;"); </code></pre> <p>Hope this help.</p> <p>Actually, It is roughly a quick summary of the <a href="http://railscasts.com/episodes/174-pagination-with-ajax?view=asciicast" rel="nofollow">railscast</a> ;-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload