0
0

More than 3 years have passed since last update.

kaminariでページネーション作ったる ❏Rails❏

Last updated at Posted at 2019-11-24

ページネーションとは

今何ページ目にいるかがわかるやつです。
よく画面の下にあります↓
7355fe4673cc916f3090de3d6f313ad7.png

kaminariというgemを使うことで簡単に実装できます。

【その1】gemを追加する

Gemfile
gem 'kaminari'
bundle install



サーバーを再起動します。

rails s

なぜなら、サーバーを起動した際にgemが反映されるからです。

【その2】コントローラーを編集する

tweets_controller.rb
def index
  @tweets = Tweet.all.page(params[:page]).per(5)
end

ページ数がpageというキーになって、パラメータとして送られます。
1ページあたりに表示する件数をper(5)のように記述します。
この場合は5件表示されます。

【その3】ビューファイルを編集する

index.html.erb
<%= paginate(@tweets) %>

好きなところに置いてください。
これでページネーションが完成します。

pagenateではなくpaginateなので注意!!



ではまた!

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