Steven
Steven

Reputation: 18014

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

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.

Upvotes: 49

Views: 32999

Answers (7)

Michael Wiltbank
Michael Wiltbank

Reputation: 141

If you are having an issue with not being able to reach root_url from a view, you can either pass it to the view, or you could also add an .env variable and then call it in the root.

asset_url('image.png', host: "https://#{ENV['HOSTNAME']}/")

Upvotes: 0

Lalit Kumar Maurya
Lalit Kumar Maurya

Reputation: 5565

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

Upvotes: 0

courtsimas
courtsimas

Reputation: 762

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

Upvotes: 6

tig
tig

Reputation: 27790

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.

Upvotes: 53

Hezad
Hezad

Reputation: 146

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

Upvotes: 4

JofoCodin
JofoCodin

Reputation: 1965

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

Upvotes: 14

Steven
Steven

Reputation: 18014

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.

Upvotes: 5

Related Questions