image_path
returns a path only (no host).
The url_for
helper won't accept a single path, so something like url_for(image_path('image.png'))
wouldn't work. While the internal url_for
in ActionDispatch::Http::Url.url_for
would appear to do the trick (source), there doesn't seem to be a public interface for it.
How should I go about doing it? Ultimately, it'd be nice to have a function like image_url
that works like url_for
does for routes so that I could call image_url('image.png')
and get the absolute URL given all of the default_url_options
.
def image_url(source)
URI.join(root_url, image_path(source))
end
This way you get url either using assets_host
or joining with root_url.
Try this in your application_helper.rb
(from one of the comments on the page Spike listed):
def image_url(source)
"#{root_url}#{image_path(source)}"
end
Our production and staging assets are on s3/cloudfront... but not locally/dev. So I wrote this (may be overkill, and probably can be simplified):
def get_digest_file(source)
return asset_path(source.to_s.downcase) unless Rails.application.config.assets.digests.present?
return ActionController::Base.asset_host + "/assets/" + Rails.application.config.assets.digests[source.to_s.downcase]
end
It would appear that as of recently, sass-rails
now interprets the image_url
command within a scss file in the expected manner, resolving to the final location of the image in question.
From Full url for an image-path in Rails 3
request.protocol + request.host_with_port + image_path('image.png')
You can even create a helper to DRY it, something like
def full_image_path(img_path)
request.protocol + request.host_with_port + image_path(img_path)
end
You can define css with absolute url for any background image or other assets. So you can generate it dynamically following this How to get absolute image paths in your css files using sass/scss.
CSS
body {
background-image: image-url(background.png);
}
In your environment file change it with yours
config.action_controller.asset_host = "http://your.domain.com/"
Then your css will look something like that:
body {
background-image: url(http://your.domain.com/assets/background.png);
}