Excon::Error::BadRequest in UsersController#update Expected(200) <=> Actual(400 Bad Request)とのエラーが出てくる
Q&A
Closed
解決したいこと
Excon::Error::BadRequest in UsersController#update
Expected(200) <=> Actual(400 Bad Request)のエラーを無くしてS3に画像のアップロードができるようにしたいです。
例)
Ruby on Railsでツイッターのような投稿型Webアプリをつくっています。
ユーザーのプロフィール画像をS3に保存する機能の実装中にエラーが発生しました。
解決方法を教えて下さい。
発生している問題・エラー
該当するソースコード
users_contoroller.rb
def update
@user = User.find_by(id: params[:id])
@user.name = params[:name]
@user.email = params[:email]
@user.avatar = params[:avatar]
if @user.save
flash[:notice] = "ユーザー情報を編集しました"
redirect_to("/users/#{@user.id}")
else
render("users/edit")
end
end
avatar_uploader.rb
class AvatarUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
# storage :file
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url(*args)
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
"default.jpg"
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
process :resize_to_limit => [700, 700]
# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end
# Add an allowlist of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_allowlist
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
carrierwave.rb
require 'carrierwave/storage/abstract'
require 'carrierwave/storage/file'
require 'carrierwave/storage/fog'
CarrierWave.configure do |config|
config.storage :fog
config.fog_provider = 'fog/aws'
config.fog_directory = 'バケット名' # 作成したバケット名を記述
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], # 環境変数
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], # 環境変数
region: 'ap-northeast-1', # アジアパシフィック(東京)を選択した場合
path_style: true
}
end
storage.yml
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>
amazon:
service: S3
region: ap-northeast-1
bucket: バケット名
gem 'fog-aws'
gem 'aws-sdk-s3'
gem 'carrierwave', '~> 2.0'
gem 'mini_magick', '~> 4.8 '
gem 'dotenv-rails'
0