- Read Tutorial
- Watch Guide Video
Audit logs and posts can run into thousands quickly, so we need pagination to help users navigate from page to page. To do that, we are going to use a gem called Kaminari
, I personally like this gem because it gives a Javascript type of implementation, which means, you'll never have to leave the page while paginating.
Open your gemfile
and copy this:
# Gemfile gem 'kaminari', '~>0.17.0'
Then run bundle
.
Next, let's start our pagination from posts. Go to the posts index page and put this code at the bottom of the file.
<!-- app/views/posts/index.html.erb --> <%= paginate @posts %>
Then open the posts_controller.rb
and add this method call:
# app/controllers/posts_controller.rb def index @posts = Post.posts_by(current_user).page(params[:page]).per(10) end
Open the browser now, and it's all working!
We're going to do the exact same thing for audit log too. Open audit_log_controller.rb
and add this code:
# app/controllers/audit_logs_controller.rb class AuditLogsController < ApplicationController def index @audit_logs = AuditLog.page(params[:page]).per(10) authorize @audit_logs end end
Likewise, include the <%= paginate @audit_logs %>
code at the very end of the audit log's index file.
Now, the audit log has a fully functioning pagination feature as well.