0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ページネーション機能の追加

Posted at

①gamを記述します。

Gemfile
gem 'kaminari', '~> 0.17.0'

②ターミナルでコマンド入力

ターミナル
$ bundle install

③コントローラーに2行追記します。

tweets_controller.rb
class TweetsController < ApplicationController
  before_action :set_tweet, only: [:edit, :show]
  PER = 8 #1ページの表示数

  def index
    @tweets = Tweet.includes(:user).order("created_at DESC")
    @pages = Tweet.page(params[:page]).per(PER)#追記
  end

④viewsファイルに1行追記します。こちらで完了です。

index.html.erb
<div class="card__summary">
  <div class="card__wrapper">
    <% @tweets.each do |tweet| %>
          <%= render partial: "tweets/tweet", locals: { tweet: tweet } %>
    <% end %>
  </div>
  <%= paginate @pages %>#追記
</div>

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?