[Ruby on Rails 3, Ruby on Rails 4] gems: will_paginate
ページネーション機能をRailsに加えるプラグイン。
gem 'will_paginate'
def index
@orders = Order.paginate page: params[:page],
order: 'created_at desc',
per_page: 10
respond_to do |format|
format.html # index.html.erb
format.json { render json: @orders }
end
end
<p><%= will_paginate @orders %></p>
生成されたページネーション。
http://localhost:3000/orders で確認。
<p><div class="pagination"><span class="previous_page disabled">← Previous</span> <em class="current">1</em> <a rel="next" href="/orders?page=2">2</a> <a href="/orders?page=3">3</a> <a href="/orders?page=4">4</a> <a href="/orders?page=5">5</a> <a href="/orders?page=6">6</a> <a href="/orders?page=7">7</a> <a href="/orders?page=8">8</a> <a href="/orders?page=9">9</a> <a href="/orders?page=10">10</a> <a class="next_page" rel="next" href="/orders?page=2">Next →</a></div></p>