Jeff Caros
Jeff Caros

Reputation: 989

<img src=""> Isn't accepting image path

I have an image in my app/assets/images/ folder called "Flower.jpg". <%=image_tag("Flower.jpg")%> works to display it, but I want to display it with html. <img src="Flower.jpg"> doesn't do anything.

For my model User I'm also using Paperclip, which stores images in public/system/users/avatars/000/000, and User.avatar_file_name returns the absolute file path, which in this case is this: /system/users/avatars/000/000/157/thumb/Flower.jpg?1248273956. Inserting this path into src like this: <img src="<%[email protected]_file_name%>"> works to display the image.

I found the absolute path of Flower.jpg in my rails console:

> helper.image_tag("Flower.jpg")
=> "<img alt=\"Flower\" src=\"/assets/Flower.jpg\" />

But then <img src="/assets/Flower.jpg\"> doesn't display, even though <img src="/system/users/avatars/000/000/157/thumb/Flower.jpg?1248273956"> does. Why is that?

Upvotes: 3

Views: 24340

Answers (1)

Max Williams
Max Williams

Reputation: 32945

The image_tag helper prepends the path to the standard images folder into the path to the file. In my version of rails this is "/images/", so this

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

is rendered out as

<img src="/images/Flower.jpg">

As with all resource paths like this, the actual file should be relative to your apps' public folder, ie

<RAILS ROOT>/public/images/Flower.jpg

In your version of rails it looks like the default path is /app/assets/images/, so this

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

is rendered out as

<img src="/app/assets/images/Flower.jpg">

which points to this file:

<RAILS ROOT>/public/app/assets/images/Flower.jpg

If you do

<img src="Flower.jpg">

it will expect the file to be in <RAILS ROOT>/public/Flower.jpg, and obviously it's not there, so the img tag won't work. If you want to write it out yourself, without using a helper, then give the path to the file from the <RAILS ROOT/public folder, ie

<img src="/app/assets/images/Flower.jpg">

BTW in your snippet of html you put scr=" instead of src=" which is going to break it even more.

Upvotes: 5

Related Questions