Rails標準のページネーションに差し替え

This commit is contained in:
Rikuoh Tsujitani 2025-02-22 22:33:09 +09:00
parent 535c812fa8
commit 6ba87e4ee7
Signed by: riq0h
GPG key ID: 010F09DEA298C717
8 changed files with 25 additions and 28 deletions

View file

@ -72,5 +72,3 @@ group :test do
end
gem 'tailwindcss-rails', '~> 4.0'
gem 'pagy'

View file

@ -160,7 +160,6 @@ GEM
racc (~> 1.4)
nokogiri (1.18.3-x86_64-linux-musl)
racc (~> 1.4)
pagy (9.3.3)
pp (0.6.2)
prettyprint
prettyprint (0.2.0)
@ -306,7 +305,6 @@ DEPENDENCIES
debug
importmap-rails
jbuilder
pagy
puma (>= 5.0)
rails (= 8.0.1)
rails-i18n

View file

@ -1,4 +1,3 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include Pagy::Backend
end

View file

@ -2,7 +2,13 @@ class MemosController < ApplicationController
before_action :set_memo, only: %i[edit update destroy]
def index
@pagy, @memos = pagy_countless(Memo.search(params[:query]), items: 12)
page = (params[:page] || 0).to_i
@memos = Memo.search(params[:query])
.order(created_at: :desc)
.limit(4)
.offset(page * 4)
@has_next = Memo.search(params[:query]).count > (page + 1) * 4
respond_to do |format|
format.html

View file

@ -1,3 +1,2 @@
module ApplicationHelper
include Pagy::Frontend
end

View file

@ -21,19 +21,17 @@
<div id="memos">
<%= turbo_frame_tag "memos" do %>
<div class="flex flex-col gap-4">
<%= render @memos %>
<%= render "empty_results", query: params[:query] if @memos.empty? %>
</div>
<% end %>
</div>
<div id="load-more" class="text-center mt-8">
<% if @pagy.next %>
<%= link_to "もっと見る",
pagy_url_for(@pagy, @pagy.next),
<div id="memos-container" class="flex flex-col gap-4">
<%= render @memos %>
<%= render "empty_results", query: params[:query] if @memos.empty? %>
</div>
<% end %>
<div id="load-more" class="text-center mt-8">
<% if @has_next %>
<%= link_to "気力十分😤",
memos_path(page: (params[:page] || 0).to_i + 1, query: params[:query]),
class: "inline-flex items-center px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white font-medium rounded-lg transition-colors duration-200",
data: { turbo_method: :get } %>
data: { turbo_frame: "memos" } %>
<% end %>
</div>
</div>

View file

@ -1,11 +1,11 @@
<%= turbo_stream.append "memos" do %>
<%= turbo_stream.append "memos-container" do %>
<%= render @memos %>
<% end %>
<%= turbo_stream.update "load-more" do %>
<% if @pagy.next %>
<%= link_to "もっと見る",
pagy_url_for(@pagy, @pagy.next),
<div id="load-more" class="text-center mt-8">
<% if @has_next %>
<%= link_to "気力十分😤",
memos_path(page: (params[:page] || 0).to_i + 1, query: params[:query]),
class: "inline-flex items-center px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white font-medium rounded-lg transition-colors duration-200",
data: { turbo_method: :get } %>
data: { turbo_frame: "memos" } %>
<% end %>
<% end %>
</div>

View file

@ -1 +0,0 @@
require 'pagy/extras/countless'