[RUBY] [Rails] About the error that the image is not displayed in the production environment

Introduction

What to do when the image is displayed firmly in the local environment, but the image disappears when deployed in the production environment.

environment

・ Rails 6.0.3. ・ Haml

Status

As shown in the notation below, the image was specified as a path using the Rails helper method ʻimage_tag`, and it was displayed well in the local environment.


= image_tag "assets/material/icon/icon.png "

However, when I deployed it, the image disappeared. (Although this notation is also suspicious)

Of course, the background-image described in the scss file was also not listed (this correspondence is described below).

Cause

In a production environment, the images will also be compiled, the image filenames will change, and the directories will change!

The difference between the production environment and the local environment is The path changes (app / assets / images / icon.png => /assets/icon.png) The name changes (icon.png => icon-xxx… .png) * xxx… is digest This will change!

Countermeasures

Use the Rails option called ʻassets_path ()`. This is convenient and can be deployed to the production environment, and even if the file name or directory changes, it can be handled locally and in production with this one!

Specifically, modify as follows


= image_tag asset_path("assets/material/icon/icon.png ")

I think this will work ... It is no longer displayed even in the local environment. .. .. ..

The result is the same even if you deploy with a bad source (naturally)

Why asset_path doesn't work

The option ʻasset_path corresponds to the files directly under ʻapp / assets / images, but apparently it does not correspond to the files in the directory beyond.

Correspondence

All the images managed in the directory have been moved directly under ʻapp / assets / images`, and the notation has been corrected as follows.


= image_tag asset_path("icon.png ")

After deploying with this, the image is finally displayed! !!

In summary, the solution was to use the option ʻasset_path and place the image files directly under ʻimages.

[scss] About background-image

Again, just specifying the image file path as shown below didn't work.


background-image: url("main-image.jpg ");

Correspondence

With ʻimage-url`, it works both locally and in production.


background-image:image-url("main-image.jpg ");

You can handle it by writing it like this. Of course, specify the image directly under ʻapp / assets / images`

It is important to note here that there is no space after the : (colon). The description of SCSS was addictive because it was written with a half-width space after the colon. So I couldn't find the cause without knowing that it was wrong.

Now when I deploy it, the image is displayed normally! !!

Recommended Posts

[Rails] About the error that the image is not displayed in the production environment
[Error] The app is not displayed in the production environment
Image is not displayed in production environment
About the problem that the image is not displayed after AWS deployment
[Rails] Reset the database in the production environment
Display the background image in the production environment
The devise error message is not displayed properly.
How to solve the problem that the website image is not displayed after deploying to heroku on Rails 5
500 Internal Server Error occurs in Rails production environment
[Cloud9] Yay! You ’re on Rails! Is not displayed in the rails tutorial
When the project is not displayed in eclipse
I get Mysql2 :: Error :: ConnectionError in the production environment
[Rails] fields_for is not displayed
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
[Android Studio] About the matter that the design view is not displayed when using TextClock
[Rails] About the problem that the video thumbnail automatically created by FFmpeg is not displayed [AWS S3 + EC2 + CarrierWave + Fog]
A solution to an error that makes you angry that you are not following the MySQL default setting ONLY_FULL_GROUP_BY in a production environment and it is not unique.
Fixed the phenomenon that vuetify built in Rails is not loaded when page transition is done
About the symbol <%%> in Rails erb
About the error that occurred when adding the column name in rails (rails db: migrate, rails db: rollback, add)
Data is not registered in Rails.
The repository ... is not signed error in docker build apt-get update
[Rails] How to reset the database in production environment (Capistrano version)
Rails6: Extract the image in Action Text
The problem that the contents of params are completely displayed in the [Rails] view
About the problem that the server can not be started with rails s
Launching the production environment with docker + rails (ver5.2) and errors that occurred
Puma --Nignx is an escape route when rails s -e production -d doesn't work in the environment
Possibility when deploying to EC2 but nothing is displayed in the error log
About the case that ("b" .. "aa") could not be used in Ruby Range
(Capistrano) After deploying, I get a We're sorry… error in the production environment.
Rails5.1 + puma SSL connection in local production environment
GoogleMap is not displayed until the browser is reloaded
When nginx conf is not reflected well in AWS Elastic Beanstalk + Rails environment
[Ruby On Rails] Error in test using RSpec MySQL client is not connected
How to resolve the error'ActionView :: Template :: Error (The asset "application.css" is not present in the asset pipeline.'" When precompiling Rails assets
The list of installed apps is not displayed in getPackageManager (Android11 ​​/ API level 30)
[Rails] I want to reset everything because the data in the local environment is strange! What to do before that
[Java] Something is displayed as "-0.0" in the output
What to do about "A server is already running ..." that happened without turning off the rails server in the terminal
Ebean.update () is not executed in the inherited model.
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
[Rails] How to upload images to AWS S3 using refile and refile-s3
[Rails] About the error that the image is not displayed in the production environment
Upload Rails app image file to S3
How to use MySQL in Rails tutorial
Image is not displayed in production environment
When nginx conf is not reflected well in AWS Elastic Beanstalk + Rails environment
About the solution of the error that occurred when trying to create a Japanese file of devise in the Docker development environment
How to deal with the error yaml.scanner.ScannerError: while scanning for the next token that appeared in Rails environment construction with Docker
[Ruby on rails] When executing the heroku command, bash: heroku: command not found is displayed. [Rails tutorial]
[Rails] How to delete production environment MySQL data after putting it in the development environment
Processing when an ID that does not exist in the database is entered in the URL
About the "Oops VFS connection does not exist" error and solution in AWS Cloud9
Java Error Handling Basics-The story that catch is only picked up in the foreground
Migration error after Activerecord association in Rails5 + Docker environment (2)
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Rails must be exist error (@save is not executed)
Migration error after Activerecord association in Rails5 + Docker environment
Verification value error that occurred in a clustering environment
Note that I stumbled upon building the Rails environment
Questions about implementing the Like feature (Ajax) in Rails
SSL in the local environment of Docker / Rails / puma
[Rails] How to display an image in the view
The story that .java is also built in Unity 2018
[Ruby on Rails] credential does not work in production environment even though I saved production.key
[Rails] What to do if you accidentally install bundle in the production environment in your local environment