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?
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}"
put this in application_helper.rb
def asset_url asset
"#{request.protocol}#{request.host_with_port}#{asset_path(asset)}"
end
then you can use asset_url
in your views.
For Rails 4, and maybe earlier, use:
config.action_mailer.asset_host = 'https://assets.com'
per https://github.com/fphilipe/premailer-rails/issues/16
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With