LoginSignup
9

More than 5 years have passed since last update.

posted at

kaminari 使い方

簡単にページャーが実装できるgemであるkaminariの使い方

[前提]
スカフォードでオブジェクトを作成している。

インストール

Gemfileに下記の記述を追加した後にbundle installを行う。

gem 'kaminari'
bundle install

ページャー設定

コントローラを変更する。
indexの処理でDBから全てレコードを取得している処理を書き換える。

cats_controller.rb
@cats = Cat.order("id").page(params[:page]).per(5)    

ビューにページャーを追加する。
下記の記述を追加する。

index.html.erb
<%= paginate @cats %>

これでindexページでページャーが使えるようになる。

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
What you can do with signing up
9