ソースのみを貼っていきます。。。
carrierwaveを使ってS3に画像をアップロードできたので、備忘録で書いておきます。 *** ところどころソースに出てくる:image
には注目
/config/routes.rb
Rails.application.routes.draw do
root "users#index"
resources :users, only: [:new, :create]
end
/controllers/users_controller.rb
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to root_url, notice: 'Add User'
else
render :new
end
end
private
def user_params
params.fetch(:user, {}).permit(:image)
end
end
/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
# ストレージにS3を指定
storage :fog
# 画像ごとに保存するディレクトリを変えたいのでオーバーライド
def store_dir
# 例えばidごとにディレクトリを分けてみる
"avater/#{model.id}"
end
# ファイル名を書き換える
def filename
# 例えば avater_1.jpg みたいなファイル名にしてみる
"avater_#{model.id}.#{file.extension}" if original_filename
end
# キャッシュ先のディレクトリを指定
def cache_dir
"cache"
end
end
/config/initializers/carrierwave.rb
# /config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: '',
aws_secret_access_key: '',
region: 'ap-northeast-1', # Tokyoの場合
}
config.cache_storage = :fog # キャッシュにS3を指定
# 環境毎にバケットを分けます
case Rails.env
when 'production'
config.fog_directory = 'バケットname'
when 'staging'
config.fog_directory = 'バケットname'
when 'development'
config.fog_directory = 'バケットname'
when 'test'
config.fog_directory = 'バケットname'
end
end
/db/migrate/20181121140557_create_users.rb
class CreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
t.string :image
t.timestamps
end
end
end
/app/views/users/index.html.erb
<h1>Users Index</h1>
<%= link_to 'Add user', new_user_path %>
<ul>
<% @users.each do |user| %>
<li>
</li>
<% end %>
</ul>
/app/views/users/new.html.erb
<h1>Add User</h1>
<%= form_with model: @user, local: true do |form| %>
<div>
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div>
<%= form.label :image %>
<%= form.file_field :image %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
/app/models/user.rb
class User < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
/Gemfile
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.6'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
gem 'carrierwave',github: 'carrierwaveuploader/carrierwave'
gem "fog"
gem 'rmagick'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
参考
【Rails】ActiveStorageを使ってお手軽にファイルアップロードを試す
carrierwaveを使ってS3に画像をアップロードする