How do I get an absolute URL for an asset in Rails 3.1?
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