3
4

More than 5 years have passed since last update.

sinatra で activerecord と連携して kaminari を使用してみる

Last updated at Posted at 2015-04-03

Gemfileに設定をする。

Gemfile
...
gem 'kaminari'
...

kaminari を require して使用するメソッドを インクルードする。

app.rb
...
require 'kaminari/sinatra'

class SampleApp < Sinatra::Base

  helpers Kaminari::Helpers::SinatraHelpers

  get '/' do
    erb :top, :layout => true
    @tasks = Task.all.page(params[:page]).per(10)
  end

  ...

end

...

デフォルトの表示件数を設定したい場合にはそれぞれの model に設定することもできる。

# Task model へ設定する
class Task
  paginates_per 10
end

ページングを表示するページへの記述

view/top.rb
...
#ページング処理を行いたいloopの下に記述
<%= paginate @tasks %>
...
3
4
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
3
4