3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[ AWS ] EC2の本番環境で画像が表示されない問題

3
Last updated at Posted at 2021-03-07

RailsのポートフォリオをデプロイしたくてEC2の環境構築から始まり、
ようやく終わりが見えかけたその時に悲劇が起こった。
 
 
スクリーンショット 2021-03-07 22.11.00.png
なん..だと!?

  
仕方なくログで確かめるが、読み方がいまいち分からない。
ただあからさまに違う所を発見。

$ tail -f  production.log

13: <div class='main-image'>
14: <%= image_tag "main_dog", class:"main-dog" %>
15: </div>

app/views/pets/index.html.erb:14

ローカル環境の画像のせいでエラーが起きているらしい。
何が原因かなんて想像もつきませんでした。
 

解決方法

imagesファイルの画像はどうやら本番環境で作動するにあたり、
データがコンパイル(圧縮)されるのが原因らしい。
あとは送られるパスも変わるらしい。パスについてはほんとに謎。

コンパイルされるのが原因なので、

config/environments/production.rb
config.assets.compile = false

29行目の記述をfalseから
trueに変更してあげる。↓↓

config/environments/production.rb
config.assets.compile = true

 

次にpublic配下に/imagesを入れる。
  

そして画像を読み込むpathを書いてあげる。
(パスの指定を本番環境用に指定する)

<%= image_tag ‘/images/dog.jpg', class:"main-dog"  %>

!上記のエラーログの記述だと読み込まない!
  

記述が終わり次第、Gitにコミット、プッシュし
ターミナル(EC2内)

まとめ

自分の場合はこれでEC2の本番環境でもエラーがなくなり、無事画像が読み込めていました。一件落着です。

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?