[RUBY] Image is not displayed in production environment

I was in trouble because the image was not displayed after deployment, so I summarized it.

I hope it helps someone.

ruby '2.6.5' rails '6.0.0'

I am making an original application.

The production environment uses Heroku. After deploying to Heroku and checking the operation, an error log appeared.

12.png

I thought that the cause was that the path to the image changed from the error log in the production environment.

If it is local, it will be displayed as it is if you put it directly under assets / images / In the production environment, it was precompiled and not displayed, so an error occurred.

When I look it up In `production, Rails puts the precompiled files in public / assets. The precompiled file is treated as a static asset by the web server. Files placed in app / assets will never be used as-is in a production environment. ``

Click here for reference articles

Make a path for the production environment by referring to the above article.

_medicine.html.erb



   <%= image_tag asset_path('medicine3.jpeg'), class:"med-pic" %>

I respecified the path in this way, but in my case I got an error again.

Please point out that asset precompile is not set in the production environment.

Change before

production.rb


     config.assets.compile = false

After change

production.rb


     config.assets.compile = true

When I enabled this, I was able to display the image safely.

Summary

<< For beginners >> If the image is not displayed in the production environment, there is no loss in trying it

(1) Check if the asset precompile setting in the production environment is valid

29


   config.assets.compile = true

(2) Create Assets file in Public by precompiling

   % rails assets:precompile

③ Describe assets_path

   <%= image_tag asset_path('medicine3.jpeg'), class:"med-pic" %>

④ Deployment is complete and the image display is complete! !!

It might be better to take a quick look at what asset precompile is doing!

Recommended Posts

Image is not displayed in production environment
[Error] The app is not displayed in the production environment
[Rails] About the error that the image is not displayed in the production environment
Error in production environment (The asset "~" is not present in the asset pipeline.)
Display the background image in the production environment
When the project is not displayed in eclipse
[Rails] fields_for is not displayed
When [command not found: composer] is displayed when [composer install] is displayed in Laravel
Create a database in a production environment
Data is not registered in Rails.
About the problem that the image is not displayed after AWS deployment
How to reflect seeds.rb in production environment
When UITextView is not displayed (Swift, Xcode)
[Rails] Reset the database in the production environment
[Cloud9] Yay! You ’re on Rails! Is not displayed in the rails tutorial
rails tutorial About account activation in production environment
The devise error message is not displayed properly.
Solution for NetBeans 8.2 not working in Java 9 environment
GoogleMap is not displayed until the browser is reloaded
When nginx conf is not reflected well in AWS Elastic Beanstalk + Rails environment
500 Internal Server Error occurs in Rails production environment
The list of installed apps is not displayed in getPackageManager (Android11 ​​/ API level 30)
[Java] Something is displayed as "-0.0" in the output
Ebean.update () is not executed in the inherited model.
I get Mysql2 :: Error :: ConnectionError in the production environment
Hivernate Validator does not work in WAS Liberty environment
[Ruby on Rails] credential does not work in production environment even though I saved production.key
When rewriting the CMD of docker image of Pod in the manifest of k8s, command is not good
Yarn is not installed
Production environment and credentials.yml.enc
Directory is not created even if directory task is described in Rakefile
What to do if FacesMessage is set but not displayed
Docker does not work when DOCKER_HOST is specified in WSL2
Line breaks in the entered text are not displayed in view
Exit code 1 occurs when Rails is stopped in Docker environment
Use docker-compose.yml which is different for each environment in Makefile
[Java] final override error is not displayed at compile time
How to solve the problem that the website image is not displayed after deploying to heroku on Rails 5
[Rails Tutorial Chapter 13] Flow when using S3 for image upload in production environment (use .env so as not to publish AWS key)
[Rails] If CSS is not applied in collection_select, check if class can be specified.
Data is not registered in Rails.
Image is not displayed in production environment
When the project is not displayed in eclipse
Ebean.update () is not executed in the inherited model.
[Error] The app is not displayed in the production environment