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.
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.
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
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
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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With