ページネーションとは
今何ページ目にいるかがわかるやつです。
よく画面の下にあります↓
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なので注意!!
ではまた!