Use kaminari for pagination

This commit is contained in:
Trevor Vallender 2023-10-13 20:28:35 +01:00
parent de4eb6742b
commit 01bcdf3a2d
7 changed files with 25 additions and 2 deletions

View File

@ -14,6 +14,8 @@ gem "jbuilder"
gem "bcrypt", "~> 3.1.7" gem "bcrypt", "~> 3.1.7"
gem "bootsnap", require: false gem "bootsnap", require: false
gem "kaminari" # Pagination
group :development, :test do group :development, :test do
gem "debug", platforms: %i[ mri mingw x64_mingw ] gem "debug", platforms: %i[ mri mingw x64_mingw ]
end end

View File

@ -129,6 +129,18 @@ GEM
sshkit (~> 1.21) sshkit (~> 1.21)
thor (~> 1.2) thor (~> 1.2)
zeitwerk (~> 2.5) zeitwerk (~> 2.5)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
loofah (2.21.4) loofah (2.21.4)
crass (~> 1.0.2) crass (~> 1.0.2)
nokogiri (>= 1.12.0) nokogiri (>= 1.12.0)
@ -264,6 +276,7 @@ DEPENDENCIES
importmap-rails importmap-rails
jbuilder jbuilder
kamal kamal
kaminari
pg pg
puma puma
rails (~> 7.1) rails (~> 7.1)

View File

@ -5,7 +5,9 @@ class BlogPostsController < ApplicationController
before_action :set_blog_post, only: [:show, :edit, :update, :destroy] before_action :set_blog_post, only: [:show, :edit, :update, :destroy]
def index def index
@blog_posts = BlogPost.published.order(created_at: :desc) @blog_posts = BlogPost.published
.order(created_at: :desc)
.page(params[:page])
end end
def new def new

View File

@ -5,7 +5,9 @@ class MicropostsController < ApplicationController
before_action :set_micropost, only: [:show, :edit, :update, :destroy] before_action :set_micropost, only: [:show, :edit, :update, :destroy]
def index def index
@microposts = Micropost.all.order(created_at: :desc) @microposts = Micropost.all
.order(created_at: :desc)
.page(params[:page])
end end
def new def new

View File

@ -18,6 +18,8 @@ class BlogPost < ApplicationRecord
scope :published, -> { where(published: true) } scope :published, -> { where(published: true) }
scope :draft, -> { where(published: false) } scope :draft, -> { where(published: false) }
paginates_per 15
def to_param def to_param
slug slug
end end

View File

@ -10,4 +10,5 @@
<li><%= render blog_post %></li> <li><%= render blog_post %></li>
<% end %> <% end %>
</ul> </ul>
<%= paginate @blog_posts %>
<% end %> <% end %>

View File

@ -12,4 +12,5 @@
<li><%= render micropost %></li> <li><%= render micropost %></li>
<% end %> <% end %>
</ul> </ul>
<%= paginate @microposts %>
<% end %> <% end %>