Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've got problems using "will_paginate", installing the GEM then unistalling... a REAL PROBLEM! So I decide to program the pagination on RoR, it was not difficult so I wanted to share what I did:</p> <p><strong>Controller:</strong></p> <pre><code> def index #how many register per page i want to show @b = 30 #params via GET from URL a = params[:page].to_i #first register per page a1 = params[:page].to_i * @b #the query can be easy... @callcenters = Income.all(:joins =&gt; "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :order =&gt; "created_at DESC",:limit =&gt; "#{a1},#{@b}", :select =&gt; "radcheck.username as clave,caller_id, created_at, value") #I need to know how many pages somehow @registrostotales = Income.all(:joins =&gt; "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :select =&gt; "radcheck.username as clave,caller_id, created_at, value").count respond_to do |format| format.html # index.html.erb format.xml { render :xml =&gt; @callcenters } end end </code></pre> <p><strong>View:</strong></p> <pre><code>... Total de registros: &lt;%= numero = @registrostotales %&gt;&lt;br/&gt;&lt;br/&gt; &lt;!-- total pages --&gt; &lt;% pages = @registrostotales / @b %&gt; &lt;!-- the last page --&gt; &lt;% if pages % @b &gt; 0 %&gt;&lt;% pages = pages + 1 %&gt;&lt;% end %&gt; Paginas: &lt;% (0..(pages-1)).each do |i| %&gt; &lt;!-- href or link_to, http://xxxxxxxx/yyyy?page=i --&gt; &lt;%= link_to i+1, :action =&gt; "index", :controller =&gt; "callcenters", :page =&gt; i %&gt; &lt;% end %&gt; &lt;!-- the view.. --&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;#&lt;/th&gt; &lt;th&gt;Tel&amp;eacute;fono (ID)&lt;/th&gt; &lt;th&gt;Zona&lt;/th&gt; &lt;/tr&gt; &lt;% @callcenters.each do |callcenter| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= numero - params[:page].to_i * 30 %&gt;&lt;% numero = numero - 1 %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= callcenter.caller_id %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= Node.first(:conditions =&gt; {:calling_id =&gt; callcenter.caller_id}).description %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/table&gt; </code></pre> <p>I hope this helps to someone!</p> <p>Best Regards, ivancarrascoq</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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