Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I page json results with will_paginate
    primarykey
    data
    text
    <p>I am using Sinatra, Ruby and Postgres. I want to page (currently trying with will_paginate but alternative method suggestions welcome) the results.</p> <p>The ruby code is:</p> <pre><code>get '/inplay' do results = DB["select distinct(concat(c.first,' ', c.last)) as candidate, c.id as id, s.shortname as shortname from candidates c, shortmembers sm, shortlists s where sm.short_id = s.list_id and c.id = sm.candidate_id"].map do |row| { :id =&gt; row[:id], :candidate =&gt; row[:candidate], :shortname=&gt;row[:shortname] } end halt 200, results.to_json end </code></pre> <p>and the jquery, in the view page, is:</p> <pre><code>$(document).ready(function() { alert("script"); $('#result').html(''); $.getJSON("/inplay", function(data) { $.each( data, function( key, value ) { $('#result').append('&lt;tr&gt;&lt;td style=\"background:#E3F6CE;\"&gt;&lt;input type=\"checkbox\" id=\"case\" class=\"case\" value=' + this["id"] + '&gt;&lt;/td&gt;&lt;td&gt;&lt;a href=\"/edit/' + this["id"] + '\"&gt;' + this["candidate"] + '&lt;/a&gt;&lt;/td&gt;&lt;td&gt;' + this["shortname"] + '&lt;/td&gt;&lt;/tr&gt;'); }); }); }); </code></pre> <p>This works fine and produces a nice table. Problem is when I include:</p> <pre><code>page = params.fetch "page", 1 per_page = params.fetch "per_page", 10 </code></pre> <p>in the ruby snippet and add</p> <pre><code>.paginate(page.to_i, per_page.to_i) </code></pre> <p>it no longer works and certainly does not page. </p> <p>In the view I have tried:</p> <pre><code>&lt;%= will_paginate results %&gt; </code></pre> <p>I have also included DB.extension(:pagination) immediately after the ruby DB call.</p> <p>Questions are:</p> <ol> <li>Is the approach wrong? The paging works well with a normal dataset but not with json results. </li> <li>If the approach is correct where do I stick the snippet ".paginate(page.to_i.per_page.to_i)" ? I have tried chaining it after .map and after .to_json with no success. </li> </ol> <p>I am stuck. All help cheerfully received. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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