Cannon Moyer
Cannon Moyer

Reputation: 3174

Get Public Asset Image Link From Rails 5

I'm trying to generate an xml file for Google Product Feed. In the xml file I want to display a link to the product image. The code

image_tag("image.png") 

works fine in my view to display the image in the browser. However I need to get the link to the asset. After running

rake assets:precompile

rails 5 generates the assets folder in my public folder with the image renamed with a hash. So going to mydomain.com/assets/image.png doesn't work However going to mydomain.com/assets/imagewithhash.png does work.

I've tried using

image_path("image.png") 

to get the hashed image name but this doesn't work. This returns

/assets/image.png

instead of

/assets/imagewithhash.png

which is what I need. Can anyone help with this issue?

Thanks in advance!

Upvotes: 2

Views: 3313

Answers (1)

Jared
Jared

Reputation: 1184

You can use asset_url('my_image.png', type: :image) or image_url('my_image.png') to get the hashed URL for your assets.

If it is not giving you the hash after the asset then Rails did not find that asset. Either you have a typo in your string or the asset does not exist. image_url('my_iamge.png') will link to localhost:3000/assets/my_iamge.png So double check those if you are still not getting the compiled asset you expect.

Upvotes: 4

Related Questions