LoginSignup
0
0

More than 1 year has passed since last update.

「1 - 20 / 35件 < >」のような日本語ページネーションの追加[Rails, kaminari]

Last updated at Posted at 2021-09-24

はじめに

以下のようなページネーションを作成しました。
実現したコードを記載したいと思います。

スクリーンショット 2021-09-24 17.34.36.png

コード

app/views/tests.html.erb
<%= page_entries_info @tests %>
<% if @tests.total_count > @tests.limit_value %>
  <%= link_to '<', path_to_prev_page(@tests) %>
  <%= link_to '>', path_to_next_page(@tests) %>
<% end %>
app/controllers/test_controller.rb
def index
    @tests = Test.all.page(params[:page])
config/kaminari_config.yml
Kaminari.configure do |config|
  config.default_per_page = 20
  # 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
end

config/locales/kaminari_ja.yml
ja:
  helpers:
    page_entries_info:
      one_page:
        display_entries:
          zero: ""
          one: "1-1/1件"
          other: "1-%{count}/%{count}件"
      more_pages:
        display_entries: "%{first}-%{last}/%{total}件"

ポイント

次のページと前のページのボタンに関しては、CSSで操作できるよう、リンクだけkaminariに排出してもらうようにしています。

path_to_prev_page(@tests) # リンクを吐き出す

またどのページでもボタンが表示されてしまう為、条件分岐でボタンは表示と非表示を切り替えています。

参考

最後までお読みいただきありがとうございました!

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