내가 한 노력들

[ Ruby on Rails ] 유용한 라이브러리 사용방법 본문

IT 공부/Ruby On Rails

[ Ruby on Rails ] 유용한 라이브러리 사용방법

JONGI-N CHOI 2021. 9. 18. 17:12

Rails 사용에 유요한 Gem


gem 'rails_db'


rails_db를 설치하게 되면, root URL뒤에 "rails/db"를 추가해주면, 시각적으로 DB를 관리할 수 있다. 


gem 'devise'

devise는 로그인 관련팩이다.

 

devise를 설치하면 다양한 기능을 사용할 수 있는데, 자주 사용되는 것

user_signed_in?

현재 로그인이 되어있는지 아닌지 확인할 수 있다. true / false

 

current_user

 

현재 로그인된 사용자의 id값을 반환

rails generate devise:views

로그인 관련 views파일들을 생성하여, 수정할 수 있다.

 

참고자료
https://github.com/heartcombo/devise

 


gem 'rails-i18l'

rails의 언어팩을 바꿀 수 있다. 

설정방법


application.rb

config.i18n.default_locale = :ko

를 추가해주면 된다.
ko는 한국어를 의미하고 다른 언어로 설정가능하다 ex) ja: 일본어, en: 영어

 



gem 'kaminari', :git => 'https://github.com/kaminari/kaminari'
pagination기능을 쉽게 구현할 수 있도록 도와주는 라이브러리

사용 방법
controller#index

@videos = Video.page(params[:page]).per(4)

 

per은 페이지마다 가져올 data의 수를 정해준다. 


index.html.erb

<%= paginate @videos %>


Theme 설정 

rails g kaminari:views bootstrap4

 

기본 kaminari에는 디자인이 없기 때문에, 새로운 theme를 추가해줘서 사용하면 이쁜 디자인 사용가능
위에는 bootstrap4를 사용했는데, 다른 theme도 사용가능하다. 

 

 

참고자료
https://github.com/kaminari/kaminari

 

 


gem 'bootstrap'

bootstap를 사용가능하도록 해준다. 



설치방법

app/assets/stylesheets/application.scss

@import "bootstrap";


참고문헌
https://github.com/twbs/bootstrap-rubygem

 



gem 'carrierwave', '~> 2.0'

 

파일을 업로드할 때 사용하는 라이브러리

사용방법


업로더 생성

rails generate uploader Model

 

모델에서 mount 

class User < ApplicationRecord
  mount_uploader :model, ModelUploader
end


마이그레이션 추가

rails g migration add_file_to_users file:string
rails db:migrate




이미지 or 파일 불러오는 법

이미지

<%= image_tag video.image.url, class: "w-full h-full" if video.image? %>


video에 image가 존재할 경우에만, image파일이 출력되도록 


동영상

<%= video_tag @video.file.url, controls: true ,autoplay: true if @video.file? %>

 

controls: true -> 영상 control을 가능하도록<br>
autoplay: true -> 화면이 새로고침 되었을 때, 자동적으로 영상이 플레이되도록


#### 참고자료
https://github.com/carrierwaveuploader/carrierwave