How to write pure HTML code, for example an <img> tag in ERB?

I want to replace <%= link_to image_tag ("picture.png") with something like this: <img src="/path/picture.png" /> in the .html.erb file

But when I do this, and it works without any error, but the image is not displayed on the page.

+5
source share
4 answers

So here is my answer for you:

By default, rails will serve all your resources from the public folder.

You can put the images in the assets/images folder and display them because of the assets pipeline .

However, if you want to use your own

 <img src="/path/picture.png" /> 

as you asked in your question, then you should understand that the path you specify in your src (src = "/path/picture.png") will refer to the public folder of your application.

To do the above work, picture.png will not be placed in app/assets/images/picture.png , but should be placed in public/path/picture.png .

By convention, you will want to put the image in:

 public/images/picture.png 

So you should use the img tag in your view as follows:

 <img src="/images/picture.png" /> 

This will work just fine for your question.

+3
source

You can do:

 <img src="<%= image_path('picture.png') %>" /> 

But I do not understand why this is better than:

 <%= image_tag("picture.png") %> 
+3
source

If you are using Rails 4.2, you must use the resource pipeline,

So you should use image_tag or image_path instead of src in the Html tag.

Ref.

 image_path("rails.png") # => "/assets/rails.png" <img src="<%= image_path('rails.png') %>" /> 

or

  image_tag("rails.png") # => <img alt="Rails" src="/assets/rails.png" /> 

Read about asset_pipeline

0
source

To make the picture.png image placed in the app/assets/images directory, you can also directly specify the assets directory in the image path as:

 <img src="/assets/picture.png" /> 
0
source

Source: https://habr.com/ru/post/1236142/


All Articles