0
1

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.

rails bootstrap Paginationの基本

Last updated at Posted at 2019-08-04

■ 環境

  • windows 8.1
  • Rails 5.1.6
  • ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]
  • Bundler version 1.15.3

Gemfileに以下を追加

Gemfile
gem 'will_paginate',           '3.1.6'
gem 'bootstrap-will_paginate', '1.0.0'
$ bundle update
$ bundle install
app/controllers/Users_controller.rb
class UsersController < ApplicationController
  def index
    @users = User.paginate(page: params[:page], per_page: 5)
    #per_pageで1ページに表示可能な個数を指定できる
  end
end
app/views/users/index.html.erb
<!-- 上で設定した@usersを使い表示させる -->

<% @users.each do |user| %>
  <p><%= link_to user.name, user %></p>
<% end %>
<%= will_paginate %>

<!-- ↓ will_paginateを少しいじる。詳しくは参照2に行くと良い-->
<% @users.each do |user| %>
  <p><%= link_to user.name, user %></p>
<% end %>
<%= will_paginate @users, previous_label: '&lt 前へ', next_label: '次へ &gt' %>

参照

  1. ruby on rails tutorial
  2. https://qiita.com/Nukioman/items/5e35ded48dfcab389c75
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?