Rails6でアプリを作っています。
画像投稿機能をcarrierwaveで作成し、画像を投稿する機能をつけました。
ローカルでは問題なくuploaderディレクトリに保存されていますが、herokuでは画像の保持ができないということで、AWSのS9を利用してアップロード機能を作っているのですが、'We're sorry, but something went wrong'が出るばかりでうまくいきません。
ruby
1require 'carrierwave/storage/abstract' 2require 'carrierwave/storage/file' 3require 'carrierwave/storage/fog' 4 5CarrierWave.configure do |config| 6 if Rails.env.production? 7 config.storage :fog 8 config.fog_provider = 'fog/aws' 9 config.fog_directory = 'バケット名' 10 config.fog_credentials = { 11 provider: 'AWS', 12 aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], 13 aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], 14 region: ENV['AWS_REGION'], 15 path_style: true 16 } 17 else 18 config.storage :file 19 config.enable_processing = false if Rails.env.test? 20 end 21end 22 23CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:].\-+]/ 24
Ruby
1class ImageUploader < CarrierWave::Uploader::Base 2 # Include RMagick or MiniMagick support: 3 include CarrierWave::RMagick 4 # include CarrierWave::MiniMagick 5 6 # Choose what kind of storage to use for this uploader: 7 8 if Rails.env.production? 9 storage :fog 10 else 11 storage :file 12 end 13 14 process :convert => 'jpg' 15 16 process resize_to_limit: [500, 500] 17 # Override the directory where uploaded files will be stored. 18 # This is a sensible default for uploaders that are meant to be mounted: 19 def store_dir 20 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 21 end 22 23 # Provide a default URL as a default if there hasn't been a file uploaded: 24 # def default_url(*args) 25 # # For Rails 3.1+ asset pipeline compatibility: 26 # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) 27 # 28 # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 29 # end 30 31 # Process files as they are uploaded: 32 # process scale: [200, 300] 33 # 34 # def scale(width, height) 35 # # do something 36 # end 37 38 # Create different versions of your uploaded files: 39 version :thumb do 40 process resize_to_fit: [150, 150] 41 end 42 43 # Add a white list of extensions which are allowed to be uploaded. 44 # For images you might use something like this: 45 def extension_whitelist 46 %w(jpg jpeg gif png) 47 end 48 49 # Override the filename of the uploaded files: 50 # Avoid using model.id or version_name here, see uploader/store.rb for details. 51 def filename 52 "#{secure_token}.jpg" if original_filename 53 end 54 55 protected 56 def secure_token 57 var = :"@#{mounted_as}_secure_token" 58 model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid) 59 end 60 61 62end 63
heroku
12019-09-28T02:49:21.344383+00:00 app[web.1]: D, [2019-09-28T02:49:21.344324 #4] DEBUG -- : [e2a24180-f823-4e16-b62d-3f5b265bfdbb] (1.2ms) ROLLBACK 22019-09-28T02:49:21.344812+00:00 app[web.1]: I, [2019-09-28T02:49:21.344757 #4] INFO -- : [e2a24180-f823-4e16-b62d-3f5b265bfdbb] Completed 500 Internal Server Error in 14ms (ActiveRecord: 6.7ms | Allocations: 1867) 32019-09-28T02:49:21.345448+00:00 app[web.1]: F, [2019-09-28T02:49:21.345397 #4] FATAL -- : [e2a24180-f823-4e16-b62d-3f5b265bfdbb] 42019-09-28T02:49:21.345450+00:00 app[web.1]: [e2a24180-f823-4e16-b62d-3f5b265bfdbb] ActiveModel::MissingAttributeError (can't write unknown attribute `image`): 52019-09-28T02:49:21.345452+00:00 app[web.1]: [e2a24180-f823-4e16-b62d-3f5b265bfdbb] 6
やったこと
ローカル環境ではPostモデルにimageカラムがちゃんとdb:migrateで追加されるのですが、heroku run rails consoleでherokuにデプロイしたアプリのPostモデルを確認すると、確かにimageカラムが存在せず、応急処置としてheroku run rails g migration add_image_column_to_posts image:sting→db:migrate:reset
を行っても、相変わらずimageカラムが追加されません。(マイグレーションは成功しているのです)
昨日から何度もトライしてみましたが、We're sorry画面が出続け、一向に進捗がないので質問させていただきました。
Rails初心者&AWSについては皆目見当がつかない状態ですので、お力をお貸しください!
#ローカル環境のPostモデル [1] pry(main)> Post.column_names (51.9ms) SELECT sqlite_version(*) => ["id", "user_id", "title", "body", "status", "deleted_at", "image", "created_at", "updated_at", "anonymous"]
#herokuのPostモデル [1] pry(main)> Post.column_names => ["id", "user_id", "title", "body", "status", "created_at", "updated_at", "deleted_at", "anonymous"]
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。