Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 682
bgadoci Avatar asked Nov 20 '09 15:11

bgadoci


People also ask

How do I Paginate in Ruby on Rails?

Pagination which is a really important aspect of any web application help in dividing documents into discrete pages. In Ruby on Rails we can easily paginate data using a gem called 'will_paginate'. The will_paginate library makes adding pagination functionality to Rails apps (and other Ruby frameworks) effortless.


3 Answers

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

like image 137
brainfck Avatar answered Sep 19 '22 19:09

brainfck


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

like image 24
mmrobins Avatar answered Sep 20 '22 19:09

mmrobins


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 %>
like image 27
Simone Carletti Avatar answered Sep 17 '22 19:09

Simone Carletti