Skip to end of metadata
Go to start of metadata
You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 3
Next »
- 아래의 예제는 ruby는 '2.5.1', rails는 '5.2.3' 버전을 기준으로 작성되었습니다.
- 파일 업로드를 위해 gem에서 제공하는 'carrierwave'라는 업로더를 사용하였습니다.
- 설치 방법 1
- 설치 방법 2
- Gemfile에 gem 'carrierwave' 입력
- bundle install
1. 이미지 업로드(동영상, 파일 업로드도 동일)
1.1 모델 생성
rails generate model UploadFile # 프로젝트/app/models/upload_file.rb가 생성됨.
# upload_file.rb
class UploadFile < ApplicationRecord
mount_uploader :file, FileUploader
end
1.2 파일 업로더 생성
rails generate uploader File # 프로젝트/app/uploaders/file_uploader.rb가 생성됨.
#file_uploader.rb
Class FileUploader < CarrierWave::Uploader::Base
1.3 컨트롤러 생성
rails generate controller UploadFile # 프로젝트/app/controllers/upload_file_controller.rb가 생성됨.
1.4 데이터베이스 마이그레이션
rails generate migration AddFileToUploadFile file:string # UploadFile에 파일을 담을 'file' 필드 추가
rake db:migrate
1.5 라우터 연결
#config/routes.rb
Rails.application.routes.draw do
...
resource :upload_file #라우터와 컨트롤러 연결
...
end
1.6 수정