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