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 5 years have passed since last update.

dockerでkaminariを実装する方法。

Last updated at Posted at 2019-11-13

rails kaminariの実装方法

kaminariの実装方法は他に多くでまわっているので今回備忘録として記述していきます。

kaminariとはページネーション機能のことでよくブログにみる表示件数を何件表示するか決める機能です。

1.Gemfileにkaminariを追加する

プロジェクト配下にあるGemfileにkaminariを追加しましょう。

gem 'kaminari'

追記したら以下のコマンドを実行

docker-compose build
docker-compose up -d

これでインストール完了。

次に以下のコマンドを実行。

docker-compose exec web rails g kaminari:config
docker-compose exec web rails g kaminari:views

それから表示件数設定を変更する場合は以下の通り

config/initializers/kaminari_config.rb

config.default_per_page = 10 # 1ページに表示する項目数のデフォルト値を10に変更

  # config.default_per_page = 25

  # config.max_per_page = nil

  # config.window = 4

  # config.outer_window = 0

  # config.left = 0

  # config.right = 0

  # config.page_method_name = :page

  # config.param_name = :page

ちなみにここでコメントアウトの設定を外すの忘れない。

config/locales/ja.ymlを作成

作成したら以下のように記述

config/locales/ja.yml

ja:

  views:

    pagination:

      first: "先頭"

      last: "末尾"

      previous: "前"

      next: "次"

      truncate: "..."

viewを修正

controllerを修正する


def index

@users = User.order(:name).page params[:page]

end

viewのindexを修正

<%= paginate @users %> #ページネーションを表示したいところに記述する

ここでアプリケーションを再起動

docker-compose stop
docker-compose up -d

これでviewを確認して反映されるはず。

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?