seki_mi
@seki_mi (seki mi)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

ローカルではエラーが出ずheroku上でActionView::Template::Error (ActiveStorage::FileNotFoundError):エラーが発生

解決したいこと

画像投稿ができるアプリケーションを開発しており、
現在、heroku上で「Failed to load resource: the server responded with a status of 500 (Internal Server Error)」エラーが発生しており解決出来ずにおります。
このエラーについてheroku上のデータベースをリセットして起動すると問題なく動作することができます。
ユーザー登録、投稿、投稿の編集、削除など一連の挙動を確認することが可能ですが、一定時間が経ってからブラウザを立ち上げると上記のエラーが発生してしまいます。
また、ローカル環境では問題なく挙動確認をすることもできエラーは発生しません。

以上です。

ご教授頂けると幸いです。

開発環境

フロントエンド
ruby 2.6.5
rails 6.0.3.5
JavaScript

データベース
MySQL

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

検証ツール上
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
ターミナル上
2021-03-31T14:00:35.780462+00:00 heroku[web.1]: Idling
2021-03-31T14:00:35.783026+00:00 heroku[web.1]: State changed from up to down
2021-03-31T14:00:36.917971+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-03-31T14:00:37.002613+00:00 app[web.1]: - Gracefully stopping, waiting for requests to finish
2021-03-31T14:00:37.004301+00:00 app[web.1]: === puma shutdown: 2021-03-31 14:00:37 +0000 ===
2021-03-31T14:00:37.004304+00:00 app[web.1]: - Goodbye!
2021-03-31T14:00:37.004504+00:00 app[web.1]: Exiting
2021-03-31T14:00:37.105646+00:00 heroku[web.1]: Process exited with status 143
2021-03-31T23:25:53.114316+00:00 heroku[web.1]: Unidling
2021-03-31T23:25:53.136872+00:00 heroku[web.1]: State changed from down to starting
2021-03-31T23:26:12.998825+00:00 heroku[web.1]: Starting process with command `bin/rails server -p ${PORT:-5000} -e production`
2021-03-31T23:26:25.975033+00:00 app[web.1]: => Booting Puma
2021-03-31T23:26:25.975073+00:00 app[web.1]: => Rails 6.0.3.5 application starting in production
2021-03-31T23:26:25.975074+00:00 app[web.1]: => Run `rails server --help` for more startup options
2021-03-31T23:26:29.711408+00:00 app[web.1]: Puma starting in single mode...
2021-03-31T23:26:29.712043+00:00 app[web.1]: * Version 3.12.6 (ruby 2.6.5-p114), codename: Llamas in Pajamas
2021-03-31T23:26:29.712103+00:00 app[web.1]: * Min threads: 5, max threads: 5
2021-03-31T23:26:29.712159+00:00 app[web.1]: * Environment: production
2021-03-31T23:26:29.712888+00:00 app[web.1]: * Listening on tcp://0.0.0.0:37708
2021-03-31T23:26:29.713744+00:00 app[web.1]: Use Ctrl-C to stop
2021-03-31T23:26:29.844938+00:00 heroku[web.1]: State changed from starting to up
2021-03-31T23:26:32.268537+00:00 app[web.1]: I, [2021-03-31T23:26:32.268382 #4]  INFO -- : [cec0ad5d-a5a7-48ef-9012-d077645935f0] Started GET "/" for 27.136.196.149 at 2021-03-31 23:26:32 +0000
2021-03-31T23:26:32.274467+00:00 app[web.1]: I, [2021-03-31T23:26:32.274336 #4]  INFO -- : [cec0ad5d-a5a7-48ef-9012-d077645935f0] Processing by SweetsController#index as HTML
2021-03-31T23:26:32.291061+00:00 app[web.1]: I, [2021-03-31T23:26:32.290896 #4]  INFO -- : [cec0ad5d-a5a7-48ef-9012-d077645935f0] Filter chain halted as :basic_auth rendered or redirected
2021-03-31T23:26:32.296240+00:00 app[web.1]: I, [2021-03-31T23:26:32.296090 #4]  INFO -- : [cec0ad5d-a5a7-48ef-9012-d077645935f0] Completed 401 Unauthorized in 16ms (Allocations: 296)
2021-03-31T23:26:32.298631+00:00 heroku[router]: at=info method=GET path="/" host=---------------.herokuapp.com request_id=cec0ad5d-a5a7-48ef-9012-d077645935f0 fwd="27.136.196.149" dyno=web.1 connect=1ms service=37ms status=401 bytes=511 protocol=https
2021-03-31T23:26:34.958857+00:00 app[web.1]: I, [2021-03-31T23:26:34.958699 #4]  INFO -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01] Started GET "/" for 27.136.196.149 at 2021-03-31 23:26:34 +0000
2021-03-31T23:26:34.962135+00:00 app[web.1]: I, [2021-03-31T23:26:34.962019 #4]  INFO -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01] Processing by SweetsController#index as HTML
2021-03-31T23:26:35.232359+00:00 app[web.1]: D, [2021-03-31T23:26:35.232168 #4] DEBUG -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]    (9.7ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
2021-03-31T23:26:35.265071+00:00 app[web.1]: I, [2021-03-31T23:26:35.264938 #4]  INFO -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]   Rendering sweets/index.html.erb within layouts/application
2021-03-31T23:26:35.277866+00:00 app[web.1]: I, [2021-03-31T23:26:35.277719 #4]  INFO -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]   Rendered shared/_header.html.erb (Duration: 10.5ms | Allocations: 528)
2021-03-31T23:26:35.304265+00:00 app[web.1]: D, [2021-03-31T23:26:35.304103 #4] DEBUG -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]   Sweet Load (21.5ms)  SELECT `sweets`.* FROM `sweets` ORDER BY created_at DESC
2021-03-31T23:26:35.504227+00:00 app[web.1]: D, [2021-03-31T23:26:35.504055 #4] DEBUG -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]   User Load (14.0ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 4
2021-03-31T23:26:35.790431+00:00 app[web.1]: D, [2021-03-31T23:26:35.790284 #4] DEBUG -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]   ActiveStorage::Attachment Load (8.4ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 24 AND `active_storage_attachments`.`record_type` = 'Sweet' AND `active_storage_attachments`.`name` = 'image' LIMIT 1
2021-03-31T23:26:35.926143+00:00 app[web.1]: D, [2021-03-31T23:26:35.925978 #4] DEBUG -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]   ActiveStorage::Blob Load (20.0ms)  SELECT `active_storage_blobs`.* FROM `active_storage_blobs` WHERE `active_storage_blobs`.`id` = 24 LIMIT 1
2021-03-31T23:26:36.006089+00:00 app[web.1]: D, [2021-03-31T23:26:36.005943 #4] DEBUG -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]   Disk Storage (0.2ms) Checked if file exists at key: variants/dyg2ybhnf3bxfkgyin9phnggjiks/4bc3ac8c2f3a5b8056e07966171d50f4592dc46b3610e7e0886d03a722b541aa (no)
2021-03-31T23:26:36.007315+00:00 app[web.1]: I, [2021-03-31T23:26:36.007232 #4]  INFO -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]   Disk Storage (0.4ms) Downloaded file from key: dyg2ybhnf3bxfkgyin9phnggjiks
2021-03-31T23:26:36.007713+00:00 app[web.1]: I, [2021-03-31T23:26:36.007584 #4]  INFO -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]   Rendered shared/_index.html.erb (Duration: 728.0ms | Allocations: 24400)
2021-03-31T23:26:36.008054+00:00 app[web.1]: I, [2021-03-31T23:26:36.007984 #4]  INFO -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]   Rendered sweets/index.html.erb within layouts/application (Duration: 742.8ms | Allocations: 25935)
2021-03-31T23:26:36.012457+00:00 app[web.1]: I, [2021-03-31T23:26:36.012350 #4]  INFO -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01] Completed 500 Internal Server Error in 1047ms (ActiveRecord: 256.5ms | Allocations: 29058)
2021-03-31T23:26:36.027078+00:00 app[web.1]: F, [2021-03-31T23:26:36.026947 #4] FATAL -- : [a5d10e1d-ad2a-45f1-952f-89bf94200c01]
2021-03-31T23:26:36.027081+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01] ActionView::Template::Error (ActiveStorage::FileNotFoundError):
2021-03-31T23:26:36.027082+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01]     1: <div class="grid">
2021-03-31T23:26:36.027083+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01]     2:     <% @sweet.each do |sweet| %>
2021-03-31T23:26:36.027083+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01]     3:       <div class="item-content">
2021-03-31T23:26:36.027084+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01]     4:           <%= link_to image_tag(sweet.image.variant(combine_options:{gravity: :center, resize:"320x320^",crop:"320x320+0+0"}).processed,class:"img-grid"), sweet_path(sweet.id) %>
2021-03-31T23:26:36.027085+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01]     5:         <div class="item-name">
2021-03-31T23:26:36.027085+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01]     6:               <%= sweet.name %>
2021-03-31T23:26:36.027086+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01]     7:           <div class="item-created_at">
2021-03-31T23:26:36.027086+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01]
2021-03-31T23:26:36.027087+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01] app/views/shared/_index.html.erb:4
2021-03-31T23:26:36.027087+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01] app/views/shared/_index.html.erb:2
2021-03-31T23:26:36.027087+00:00 app[web.1]: [a5d10e1d-ad2a-45f1-952f-89bf94200c01] app/views/sweets/index.html.erb:21
2021-03-31T23:26:36.028691+00:00 heroku[router]: at=info method=GET path="/" host=----------------.herokuapp.com request_id=a5d10e1d-ad2a-45f1-952f-89bf94200c01 fwd="27.136.196.149" dyno=web.1 connect=1ms service=1077ms status=500 bytes=1827 protocol=https
2021-03-31T23:26:36.322961+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=----------------.herokuapp.com request_id=96ca3aee-0930-4843-8c05-8f4d342f864b fwd="27.136.196.149" dyno=web.1 connect=1ms service=5ms status=200 bytes=143 protocol=https

該当するソースコード

_index.html.erb
<div class="grid">
    <% @sweet.each do |sweet| %>
      <div class="item-content">
        <% if sweet.image.variable? %>
          <%= link_to image_tag(sweet.image.variant(combine_options:{gravity: :center, resize:"320x320^",crop:"320x320+0+0"}).processed,class:"img-grid"), sweet_path(sweet.id) %>
        <% end %>

自分で試したこと

Active Storageに問題があると考えgemの確認、モデル内のアソシエーションやバリデーションの確認を行ないました。
gemについては
gem 'mini_magick'
gem 'image_processing', '~> 1.2'
を導入、bundle install済み。

sweet.rb
has_one_attached :image
validates :image, presence: true, unless: :was_attached?

def image_content_type
    extension = ['image/png', 'image/jpg', 'image/jpeg']
  end

def was_attached?
    self.image.attached?
  end
0

1Answer

Comments

  1. @seki_mi

    Questioner

    ありがとうございます!無事に解決することができました。

Your answer might help someone💌