1
votes

How can I get the absolute URL of an image in Rails.

Rails serves the images using assets.

So an image by the name "logo.png" is actually stored as "logo-776bfb0d3b4bdea029da753cf63916e2.png".

This is not a problem when I need to access the image using a Rails server as I render it using the <%= image_tag %> helper.

But I need to pass the actual URL to an API which will then render the image.

So how can I just get the physical URL??

3

3 Answers

0
votes

This is the name of the file. It will be in your public/assets directory and the url will be your-site-name/assets/logo-776bfb0d3b4bdea029da753cf63916e2.png.

Note this will change when you precompile your assets and the file is given a new fingerprint. You will be much better off using the rails image_url helper to give you the URL and passing that to your API.

0
votes

If you are using rails 4. Then the image will be in name_of_your_app/app/assets/images/.

0
votes

You probably have to include ActionView::Helpers::AssetUrlHelper. Or you could just call ActionController::Base.helpers.image_tag("image.png")