Rails 3.1 absolute URL to an image

alik picture alik · Sep 29, 2011 · Viewed 32.1k times · Source

I'm using Rails 3.1. I'm trying to figure this out, and to my surprise, it is starting to seem that rails does not come with this method at all. Maybe im wrong.

Can anyone show how I can get a full absolute URL to an image?

I use asset_path(image.png) which gives me the relative path to use within the app. I tried doing a root_url + asset_path(image.png) but that just gives me a http://localhost:3000//assets/image.png with the double slashes

Anyone have an efficient way of doing this?

Answer

Simone Carletti picture Simone Carletti · Sep 29, 2011

See the Using asset hosts section in the documentation. You need to specify an asset_host. You can also construct it dynamically from the request chaining "#{request.protocol}#{request.host_with_port}"