bgadoci
bgadoci

Reputation: 6493

Adding pagination to index.html.erb in Ruby on Rails - Easy Question

I am new to rails so go easy. I have built my first blog and would like to set it up such that in the <% post.each do |post| %> render of the posts, I would like it to work such that it only displays 10 posts and then has a link to view the next 10.

I am hoping this is an easy question. Let me know if you would like me to post any code.

Upvotes: 5

Views: 29768

Answers (8)

Ivan Carrasco Quiroz
Ivan Carrasco Quiroz

Reputation: 605

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:

Controller:

  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 => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :order => "created_at DESC",:limit => "#{a1},#{@b}", :select => "radcheck.username as clave,caller_id, created_at, value")
#I need to know how many pages somehow
     @registrostotales = Income.all(:joins => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :select => "radcheck.username as clave,caller_id, created_at, value").count

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @callcenters }
    end
  end

View:

...
Total de registros: <%= numero = @registrostotales %><br/><br/>
<!-- total pages -->
<% pages = @registrostotales / @b %>
<!-- the last page -->
<% if pages % @b > 0 %><% pages = pages + 1 %><% end %>

Paginas:
<% (0..(pages-1)).each do |i| %>
<!-- href or link_to, http://xxxxxxxx/yyyy?page=i -->
  <%= link_to i+1, :action => "index", :controller => "callcenters", :page => i %>
<% end %>


<!-- the view.. -->
<table>
  <tr>
    <th>#</th>
    <th>Tel&eacute;fono (ID)</th>
    <th>Zona</th>
  </tr>

<% @callcenters.each do |callcenter| %>
  <tr>
    <td><%= numero - params[:page].to_i * 30 %><% numero = numero  - 1 %></td>
    <td><%= callcenter.caller_id %></td>
    <td><%= Node.first(:conditions => {:calling_id => callcenter.caller_id}).description %></td>
  </tr>
<% end %>
</table>

I hope this helps to someone!

Upvotes: 5

lumpidu
lumpidu

Reputation: 759

Or, small, swifd & actively maintained: Pagy

Upvotes: 0

brainfck
brainfck

Reputation: 9376

You should use the gem will_paginate.

The installation and usage is really easy: https://github.com/mislav/will_paginate/wiki/installation

Example usage: http://github.com/mislav/will_paginate

Upvotes: 8

sivamani
sivamani

Reputation: 535

Adding pagination to a Ruby on Rails app

To add pagination to your Ruby on Rails app, you have to modify the following files:

Gemfile:

gem 'will_paginate', '~> 3.1.0'
gem 'will_paginate-bootstrap'

Areas controller --> index

@areas = Area.pagination_request(params[:page])

index.html.erb

<%= will_paginate @areas, renderer: BootstrapPagination::Rails %>

Model file:

def self.pagination_request(page)
    paginate :per_page => 10, :page => page
end

Upvotes: 0

Ashish Garg
Ashish Garg

Reputation: 125

You can use the kaminari gem.

Very ease to use and highly customization friendly.

Upvotes: -1

Chaithanya Krishna
Chaithanya Krishna

Reputation: 1484

We can do this with ease by using 'will_paginate-bootstrap' gem.

  1. To continue firstly you add a line to the gem file as,

    gem 'will_paginate-bootstrap'.

    Now run bundle install.

  2. In the controller, you will be using like @post = Post.all to get the contents from models. Instead use this

@post = Post.paginate(:page=>params[:page],per_page:5)

Here in the above line per_page field is to specify how many records you need in a page.

  1. In index.html.erb At the end of the code i.e before ending the body tag Add this,

<%= will_paginate @post,renderer: BootstrapPagination::Rails %>

Note: I thought Post as modal and post as variable declared in the controller. Just go with your variables.

Upvotes: 0

mmrobins
mmrobins

Reputation: 13765

will_paginate is definitely the way to go, I just thought I'd add a link to a railscasts will_paginate video showing you how to use it since sometimes that can be an easier way to learn the basics than reading documentation. Plus you'll learn a brief history on how pagination used to be done when it was an included part of Rails (it was slower and more cumbersome). The old classic pagination has been moved out into it's own plugin too, but it's only recommended if you were already using it when you upgraded Rails to a version where they took it out.

Railscasts has a ton of other great videos that you might find helpful. For example, once you get more advanced you might want to try ajax pagination

Upvotes: 7

Simone Carletti
Simone Carletti

Reputation: 176362

Check out the will_paginate Gem.

It’s very easy to do pagination on ActiveRecord models:

@posts = Post.paginate :page => params[:page], :order => 'created_at DESC'

In the view, page links can be rendered with a single view helper:

<%= will_paginate @posts %>

Upvotes: 5

Related Questions