Herokuデプロイするも、seedの画像データが読み込まれない
Q&A
Closed
デプロイしたアプリにseedの画像データを反映させたい
画像表示には、ActiveStorageを使用しており、保存先にはAWS S3を指定しています。
config/storage.yml
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>
# Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
amazon:
service: S3
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
region: <%= ENV['AWS_REGION'] %>
bucket: <%= ENV['AWS_BUCKET'] %>
config/environments/production.rb
〜
# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :amazon
〜
今回新しいモデルとseedデータを追加したためデプロイ後に「heroku run rails db:migrate」を実行。ここまでは、特にエラーありません。
> heroku run rails db:migrate
Running bin/rails db:migrate on ⬢ foodmatchapp... up, run.2968 (Basic)
〜
発生している問題・エラー
その後、「heroku run db:seed」を実行すると、そのような画像は見つからないよとエラーをはかれました。
> heroku run rails db:seed
rails aborted!
Errno::ENOENT: No such file or directory @ rb_sysopen - /app/app/assets/images/ingredients/mushrooms/マツタケ.JPG
/app/db/seeds.rb:42:in `initialize'
/app/db/seeds.rb:42:in `open'
/app/db/seeds.rb:42:in `block in <main>'
/app/db/seeds.rb:40:in `each'
/app/db/seeds.rb:40:in `<main>'
〜
seedは、次のように指定していました。
seeds.rb
ingredients = [
[ 1, "マツタケ", "高級、香りが強いのが特徴。", "", 1, "mushrooms" ],
[ 2, "シイタケ", "一般的、焼くととても美味しい。", "夏バテ", 1, "mushrooms" ],
[ 3, "マイタケ", "よく煮込み料理に使われることがある。", "夏バテ", 1, "mushrooms" ],
[ 4, "しめじ", "低価格かつ歯ごたえがあり食べ応えもある。", "", 1, "mushrooms" ],
]
ingredients.each do |id, name, flavor_text, cooking_effect, category_id, category_name|
ingredient = Ingredient.find_or_create_by!(id: id, name: name, flavor_text: flavor_text, cooking_effect: cooking_effect, category_id: category_id)
ingredient.image.attach(io: File.open(Rails.root.join("app/assets/images/ingredients/#{category_name}/#{name}.JPG")), filename: "#{name}.JPG")
end
※ デプロイしたアプリ自体に画像が入っていないのでは?と思い、「heroku run bash」でフォルダの中身を確認したところ、app/assets/images/〜に画像は入っていました。
自分で試したこと
1 エラーにもあった画像パスについて、"app"が一つ多かったのが原因ではと考え、seed内の"Rais.root.join"の記述を消しました。
seeds.rb
File.open("app/assets/images/ingredients/#{category_name}/#{name}.JPG"), filename: "#{name}.JPG")
その後、まずローカル環境で「db:migrate:reset」して再度「db:seed」を実行。
画像表示を確認したのちに、コミット、Herokuへpush。再び、「heroku run db:seed」を実行するも、同じようなエラーがはかれました。
> heroku run rails db:seed
rails aborted!
Errno::ENOENT: No such file or directory @ rb_sysopen - /app/assets/images/ingredients/mushrooms/マツタケ.JPG
〜
2 「heroku logs --tail」で、該当ページのログを確認したところ、ActionView::Template::Error (Nil location provided. Can't build URI.)というエラーが起きていました。
[aeaa51e3-859d-4f1c-931b-c67781087339] Started GET "/ingredients" for 126.253.48.68 at 2023-11-02 06:41:37 +0000
[aeaa51e3-859d-4f1c-931b-c67781087339] Processing by IngredientsController#index as HTML
[aeaa51e3-859d-4f1c-931b-c67781087339] Rendered ingredients/index.html.erb within layouts/application (Duration: 4.7ms | Allocations: 1092)
[aeaa51e3-859d-4f1c-931b-c67781087339] Rendered layout layouts/application.html.erb (Duration: 4.8ms | Allocations: 1122)
[aeaa51e3-859d-4f1c-931b-c67781087339] Completed 500 Internal Server Error in 9ms (ActiveRecord: 4.1ms | Allocations: 1705)
[aeaa51e3-859d-4f1c-931b-c67781087339]
[aeaa51e3-859d-4f1c-931b-c67781087339] ActionView::Template::Error (Nil location provided. Can't build URI.):
[aeaa51e3-859d-4f1c-931b-c67781087339] 6: <div class="bg-ingredient border rounded p-2">
[aeaa51e3-859d-4f1c-931b-c67781087339] 7: <div class="row">
[aeaa51e3-859d-4f1c-931b-c67781087339] 8: <div class="col-auto">
[aeaa51e3-859d-4f1c-931b-c67781087339] 9: <%= image_tag ingredient.image.variant(resize_to_limit: [60, 60]), alt: "#{ingredient.name}の画像" %>
[aeaa51e3-859d-4f1c-931b-c67781087339] 10: </div>
[aeaa51e3-859d-4f1c-931b-c67781087339] 11: <div class="col d-flex flex-column">
[aeaa51e3-859d-4f1c-931b-c67781087339] 12: <div class="d-flex justify-content-between">
[aeaa51e3-859d-4f1c-931b-c67781087339]
[aeaa51e3-859d-4f1c-931b-c67781087339] app/views/ingredients/index.html.erb:9
[aeaa51e3-859d-4f1c-931b-c67781087339] app/views/ingredients/index.html.erb:4
[aeaa51e3-859d-4f1c-931b-c67781087339] app/views/ingredients/index.html.erb:3
「heroku run db:migrate:rest」から、「heroku run rails db:seed」を実行するものの、同じエラーをはかれました。
補足情報
ruby 3. 1. 4
rails 7. 0. 7
heroku 8. 7. 0
色々探してみましたが、同じようなエラーの方の記事を見つけられておらず、詰まっています。
拙い内容ですが、どなたか助言いただければ幸いです。
よろしくお願いいたします。