[Ruby on Rails] 페이지 (paginate) Gem: kaminari
2022. 6. 27. 22:11
반응형
Kaminari를 이용한 Pagination 구현
1. Gem 설치
Gemfile 작성
Gem 'kaminari'
$ bundle install
2. Controller에서 내용 추가
class PostsController < ApplicationController
def index
@posts = Post.order("created_at DESC").page params[:page]
end
end
3. 한 목록당 게시물 갯수 설정
class Post < ApplicationRecord
paginates_per 5
...
end
혹은
class PostsController < ApplicationController
def index
@posts = Post.order("created_at DESC").page(params[:page]).per(5)
end
def show
@posts = Post.page(params[:page]).per(10)
end
end
이런 식으로 paginate 정의를 해준다.
index.html.erb 파일에서 아래 태그를 삽입하면 목록 번호가 뜬다.
<%= paginate @posts %>
kaminari에 디자인 템플릿 적용
https://github.com/felipecalvo/bootstrap5-kaminari-views
GitHub - felipecalvo/bootstrap5-kaminari-views: Bootstrap 5 compatible styles for Kaminari. Tested on Bootstrap 5.0.1.
Bootstrap 5 compatible styles for Kaminari. Tested on Bootstrap 5.0.1. - GitHub - felipecalvo/bootstrap5-kaminari-views: Bootstrap 5 compatible styles for Kaminari. Tested on Bootstrap 5.0.1.
github.com
kaminari에 디자인 템플릿을 적용할 수 있다.
728x90
반응형
'web > Ruby on Rails' 카테고리의 다른 글
Written by ner.o
개발자 네로의 개발 일기,
자바를 좋아합니다 !
댓글 개