mochi93kou
@mochi93kou (まるも)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

railsアプリで本番環境で画像アップロードエラー

Q&A

Closed

解決したいこと

RailsアプリをHerokuにデプロイしてseedを作成しようとしたら、エラーが発生しました

環境

  • macOS Monterey 12.3.1
  • Ruby 3.1.2
  • Bundler 2.3.10
  • Rails 6.1.4.7
  • mini_magick (4.11.0)
  • carrierwave (2.2.2)
  • Heroku
  • S3

発生している問題・エラー

RailsアプリをHerokuにデプロイしてseedを作成しようとしたら、エラーが発生しました。
画像アップロードでエラーが出ていると思われます。

MicropostとProductモデルで画像をアップロードします。
Productモデルは複数投稿ができるようになっています。

ActiveRecord::RecordInvalid: バリデーションに失敗しました: MiniMagickがファイルを処理できませんでした。画像を確認してください。エラーメッセージ: `convert /app/tmp/1664890500-788499677509550-0013-8429/sioyaki.avif -auto-orient -resize 500x500> /tmp/image_processing20221004-4-uy9mte.avif` failed with error:
convert-im6.q16: no decode delegate for this image format `AVIF' @ error/constitute.c/ReadImage/560.
convert-im6.q16: no images defined `/tmp/image_processing20221004-4-uy9mte.avif' @ error/convert.c/ConvertImageCommand/3258.
, MiniMagickがファイルを処理できませんでした。画像を確認してください。エラーメッセージ: `convert /app/tmp/1664890501-786000219974198-0014-0548/mizu.avif -auto-orient -resize 500x500> /tmp/image_processing20221004-4-7nuull.avif` failed with error:
convert-im6.q16: no decode delegate for this image format `AVIF' @ error/constitute.c/ReadImage/560.
convert-im6.q16: no images defined `/tmp/image_processing20221004-4-7nuull.avif' @ error/convert.c/ConvertImageCommand/3258.
, MiniMagickがファイルを処理できませんでした。画像を確認してください。エラーメッセージ: `convert /app/tmp/1664890501-394529731584199-0015-8193/nizimasu.avif -auto-orient -resize 500x500> /tmp/image_processing20221004-4-kadtqj.avif` failed with error:
convert-im6.q16: no decode delegate for this image format `AVIF' @ error/constitute.c/ReadImage/560.
convert-im6.q16: no images defined `/tmp/image_processing20221004-4-kadtqj.avif' @ error/convert.c/ConvertImageCommand/3258.
, 選択してください
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/validations.rb:80:in `raise_validation_error'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/validations.rb:53:in `save!'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/transactions.rb:302:in `block in save!'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/transactions.rb:354:in `block in with_transaction_returning_status'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract/database_statements.rb:320:in `block in transaction'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract/transaction.rb:319:in `block in within_new_transaction'
/app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract/transaction.rb:317:in `within_new_transaction'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract/database_statements.rb:320:in `transaction'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/transactions.rb:350:in `with_transaction_returning_status'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/transactions.rb:302:in `save!'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/suppressor.rb:48:in `save!'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/persistence.rb:55:in `create!'
/app/db/seeds/production.rb:62:in `<main>'
/app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:39:in `load'
/app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:39:in `load'
/app/db/seeds.rb:1:in `<main>'
/app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:39:in `load'
/app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:39:in `load'
/app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.4.7/lib/rails/engine.rb:566:in `block in load_seed'
/app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.4.7/lib/active_support/callbacks.rb:117:in `block in run_callbacks'
/app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.4.7/lib/active_support/execution_wrapper.rb:91:in `wrap'
/app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.4.7/lib/rails/engine.rb:640:in `block (2 levels) in <class:Engine>'
/app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.4.7/lib/active_support/callbacks.rb:126:in `instance_exec'
/app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.4.7/lib/active_support/callbacks.rb:126:in `block in run_callbacks'
/app/vendor/bundle/ruby/3.1.0/gems/activesupport-6.1.4.7/lib/active_support/callbacks.rb:137:in `run_callbacks'
/app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.4.7/lib/rails/engine.rb:566:in `load_seed'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/tasks/database_tasks.rb:450:in `load_seed'
/app/vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.4.7/lib/active_record/railties/databases.rake:392:in `block (2 levels) in <main>'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
/app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.4.7/lib/rails/commands/rake/rake_command.rb:24:in `block (2 levels) in perform'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
/app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.4.7/lib/rails/commands/rake/rake_command.rb:24:in `block in perform'
/app/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/rake_module.rb:59:in `with_application'
/app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.4.7/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.4.7/lib/rails/command.rb:50:in `invoke'
/app/vendor/bundle/ruby/3.1.0/gems/railties-6.1.4.7/lib/rails/commands.rb:18:in `<main>'
/app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
/app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
/app/bin/rails:5:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
production.rb
User.create!(
  name: Rails.application.credentials[:admin][:user_name],
  password: Rails.application.credentials[:admin][:password],
  password_confirmation: Rails.application.credentials[:admin][:password],
  email: Rails.application.credentials[:admin][:email],
  admin: true,
  activated: true
)

Address.create!(
  user_id: 1,
  address_name: "管理者",
  postcode: "1231234",
  prefecture: "東京都",
  city: "台東区",
  house_number: "123-123",
  building: "テストアパート",
  saved: true
)


User.create!(
  name: "安曇野太郎",
  password: "password123",
  password_confirmation: "password123",
  email: "azumino@taro.com",
  activated: true,
  activated_at: Time.zone.now,
  reset_sent_at: Time.zone.now
)

Address.create!(
  user_id: 2,
  address_name: "安曇野太郎",
  postcode: "1231234",
  prefecture: "東京都",
  city: "台東区",
  house_number: "123-123",
  building: "テストアパート",
  saved: true
)

(1..12).each do |n|
  Micropost.create!(
    title: "#{n}月の営業日のお知らせ",
    content: "#{n+3}日,#{n+4}\n#{n+8}日,#{n+9}\n#{n+15}日,#{n+16}日はお休みになります",
    user_id: 1,
    image: File.open("./app/assets/images/news-img.avif"),
    created_at: "#{Time.zone.now - 10.month + n.month}"
  ) 
end

12.times do |n|
  Contact.create!(
    name: "recent test #{n} user",
    email: "recent#{n}@example.com",
    message: "test inquiry contact #{n} message",
    category: "商品について"
  ) 
end

Product.create!(
  name: "虹鱒の塩焼き",
  price: 400,
  images: [File.open("./app/assets/images/sioyaki.avif"),File.open("./app/assets/images/mizu.avif"),File.open("./app/assets/images/nizimasu.avif")],
  introduction: "安曇野の水で育った臭みがが少ない虹鱒を塩焼きにしました。ご飯のおかずにいかがですか"
) 

Product.create!(
  name: "虹鱒の甘露煮",
  price: 400,
  images: [File.open("./app/assets/images/kanroni.avif"),File.open("./app/assets/images/mizu.avif"),File.open("./app/assets/images/nizimasu.avif")],
  introduction: "クセがない白身が特徴の虹鱒を骨まで食べられるほどじっくりと煮て甘露煮に仕上げました。"
) 

Product.create!(
  name: "岩魚の塩焼き",
  price: 400,
  images: [File.open("./app/assets/images/iwana_sioyaki.avif"),File.open("./app/assets/images/mizu.avif"),File.open("./app/assets/images/iwana.avif")],
  introduction: "塩焼きにした岩魚は身が淡白であっさりとした味わいに加え深みが加わり最高の一品です。"
) 


Product.create!(
  name: "岩魚の甘露煮",
  price: 400,
  images: [File.open("./app/assets/images/kanroni.avif"),File.open("./app/assets/images/mizu.avif"),File.open("./app/assets/images/iwana.avif")],
  introduction: "臭みがなく身が淡白であっさりとしたのが特徴のイワナを砂糖や醤油で煮て甘露煮にしました。"
) 

Product.create!(
  name: "信州サーモンの切り身",
  price: 3000,
  images: [File.open("./app/assets/images/salmon.avif")],
  introduction: "脂は甘くあっさりとしているのが特徴の信州サーモンです。ぜひお刺身でお楽しみください。"
) 
product_uploder.rb
class ProductUploader < CarrierWave::Uploader::Base
  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  if Rails.env.development? || Rails.env.test?
    storage :file
  else
    storage :fog
  end

  # 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('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # 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 avif)
  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
  process resize_to_limit: [500, 500]
end
ImageUploder
class ImageUploader < CarrierWave::Uploader::Base
  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick
  # process resize_to_fit: [400, 200]
  
  # Choose what kind of storage to use for this uploader:
  if Rails.env.development? || Rails.env.test?
    storage :file
  else
    storage :fog
  end

  # 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('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # 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 avif)
  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


自分で試したこと

heroku run rails db:migrate:reset DISABLE_DATABASE_ENVIRONMENT_CHECK=1

で一度リセットしてから再度seed作成しても同じエラーが出ました


Productを作成する部分をコメントアウトしたら正常に作成できました。

production.rb
#ここからコメントアウト
Product.create!(
  name: "虹鱒の塩焼き",
  price: 400,
  images: [File.open("./app/assets/images/sioyaki.avif"),File.open("./app/assets/images/mizu.avif"),File.open("./app/assets/images/nizimasu.avif")],
  introduction: "安曇野の水で育った臭みがが少ない虹鱒を塩焼きにしました。ご飯のおかずにいかがですか"
) 

Product.create!(
  name: "虹鱒の甘露煮",
  price: 400,
  images: [File.open("./app/assets/images/kanroni.avif"),File.open("./app/assets/images/mizu.avif"),File.open("./app/assets/images/nizimasu.avif")],
  introduction: "クセがない白身が特徴の虹鱒を骨まで食べられるほどじっくりと煮て甘露煮に仕上げました。"
) 

Product.create!(
  name: "岩魚の塩焼き",
  price: 400,
  images: [File.open("./app/assets/images/iwana_sioyaki.avif"),File.open("./app/assets/images/mizu.avif"),File.open("./app/assets/images/iwana.avif")],
  introduction: "塩焼きにした岩魚は身が淡白であっさりとした味わいに加え深みが加わり最高の一品です。"
) 


Product.create!(
  name: "岩魚の甘露煮",
  price: 400,
  images: [File.open("./app/assets/images/kanroni.avif"),File.open("./app/assets/images/mizu.avif"),File.open("./app/assets/images/iwana.avif")],
  introduction: "臭みがなく身が淡白であっさりとしたのが特徴のイワナを砂糖や醤油で煮て甘露煮にしました。"
) 

Product.create!(
  name: "信州サーモンの切り身",
  price: 3000,
  images: [File.open("./app/assets/images/salmon.avif")],
  introduction: "脂は甘くあっさりとしているのが特徴の信州サーモンです。ぜひお刺身でお楽しみください。"
) 
#ここまで


0

1Answer

product_uploader.rb
process resize_to_limit: [500, 500]

を削除することでエラーの解除ができました。
くわしくはわかりませんがavifファイルはリサイズできない?のが原因かと思われます。

0Like

Your answer might help someone💌