How do I get an absolute URL for an asset in Rails

2020-02-20 06:28发布

问题:

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.

回答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.



回答2:

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


回答3:

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


回答4:

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.



回答5:

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


回答6:

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);
}