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

8

5

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.

Manpower answered 20/11, 2009 at 15:38 Comment(0)
C
7

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

Chromomere answered 20/11, 2009 at 16:5 Comment(0)
R
8

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

Refund answered 20/11, 2009 at 15:39 Comment(0)
C
7

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

Chromomere answered 20/11, 2009 at 16:5 Comment(0)
Y
5

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 %>
Yesman answered 20/11, 2009 at 15:40 Comment(4)
Since I am new I will ask a stupid question. Do I put the first line of code in the Model or the controller. I thought model based on your answer but didn't work. Here is what I typed: def will_paginate @posts = Post.paginate :page => params[:page], :order => 'created_at DESC' endManpower
The code is intended to be placed in the controller. If you want, you can also use the #paginate method in your model to create a custom method.Yesman
Ok, thanks. Just having trouble installing plugin from errtheblog.comManpower
It's easiest to install the plugin as a gem, via RubyGems.Pamella
N
5

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!

Nucleoplasm answered 21/11, 2013 at 23:24 Comment(0)
G
0

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.

Gemstone answered 20/10, 2017 at 2:8 Comment(3)
Please use syntax highlighting!Ineptitude
Ok. Done with thatGemstone
gem is not maintained since >4 years !Angio
M
0

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
Mooring answered 24/5, 2019 at 12:9 Comment(1)
will_paginate-bootstrap is no longer maintained, last commit was on 07.11.2016Angio
A
0

Or, small, swifd & actively maintained: Pagy

Angio answered 6/1, 2020 at 22:40 Comment(0)
A
-1

You can use the kaminari gem.

Very ease to use and highly customization friendly.

Anacoluthon answered 29/5, 2019 at 12:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.