How to paginate blog posts in a Rails application
-
06-07-2019 - |
Question
I tried the following code but it doesn't work
class BlogsController < ApplicationController
def index
#@entry_pages = Paginator.new(self, Entry.count, 10, params[:page])
@entries = Entry.find(:all,
#:limit => @entry_pages.items_per_page,
#:offset => @entry_pages.current.offset,
:order => 'entries.created_at DESC',
:include => :user)
end
end
This is the blog view
<h1>Recently updated blogs</h1>
<% @entries.each do |entry| %>
<p>
<%= link_to entry.user.username, entries_url(:user_id => entry.user) %><br />
'<%= entry.title %>' was posted <%= time_ago_in_words(entry.created_at) %> ago
</p>
<% end %>
I want the items to be paginated like this:
<< [1][2][3] >>
Solution
Give the will_paginate GEM a try. It provides all the features you need to paginate your blog entries.
OTHER TIPS
I can recommend the paginating_find plugin. Here's a tutorial:
http://www.igvita.com/2006/09/10/faster-pagination-in-rails/
Looks like it's now hosted on github.com:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow