HTML img tag image is not displayed

Asked 2 years ago, Updated 2 years ago, 143 views

We are developing at Rails.
The image is positioned by the pass assets/images/name .jpg, but
If you include the above path in the src of the HTML img tag, you will not see the image.

The view file is located in the home folder in the views folder in the same hierarchy as the associates folder.

ruby-on-rails image

2022-09-30 21:37

1 Answers

To view the image files in associates, use image_tag to specify:The appropriate img tag expands.

<%=image_tag("test.jpg")%>

The file name below assets cannot be specified as it is.This is because the files in assembets are eventually referenced as /aseets/base name-digest value.extensions (e.g./assets/test-3e7e2ca170634b5950c05290d759711e800664f93cdd6914fc58590f8bd7619d.jpg).(If the contents of the file change, the digest value will change to a different file name.)

Use asset_path to get the image path for any reason.

asset_path("test.jpg")
<img src="<%=asset_path("test.jpg")%>">

If you want to use a fixed file name, you can also put it in a directory below public.In this case, public/images/test.jpg can be referenced directly as /images/test.jpg.

(Reference) Why do I include digest values in my file name?

In production, the contents of /assets are often cached for faster speeds, but the cache is not used when the contents of the file change.

If you don't want to use digest, you can change it by setting it up.Add the following to config/initializers/assets.rb:(I don't recommend it.)

Rails.application.config.assets.digest=false


2022-09-30 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.