Take the 2-minute tour ×

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.

share|improve this question
this blog has a snippet that you can use: stick.gk2.sk/blog/2009/12/image_url-function-in-ruby-on-rails –  Spike Gronim Jun 27 '11 at 23:17
@Spike: the shortcoming with that approach is that it ignores the default url options. There would, for instance, be no way to override the inferred request params either manually or through a default_url_options hash in the app configuration. –  Steven Xu Jun 28 '11 at 1:50
@SpikeGronim This will not work with delayed jobs as request will be empty –  lulalala Dec 22 '11 at 10:03

6 Answers 6

def image_url(source)
  URI.join(root_url, image_path(source))

This way you get url either using assets_host or joining with root_url.

share|improve this answer
where do you put this code? –  Syl Jul 4 '13 at 14:19
@Syl In a helper –  tig Jul 4 '13 at 17:34
for info you must add add_template_helper(MailerHelper) to the mailer to make the helper works in 3.2 –  Syl Jul 5 '13 at 8:46
@Syl Why MailerHelper? It is about assets not about mailing –  tig Jul 5 '13 at 9:54

Try this in your application_helper.rb (from one of the comments on the page Spike listed):

def image_url(source)
share|improve this answer
To remove the extra slash: def image_url(source) "#{root_url[0...-1]}#{image_path(source)}" end –  iGEL Nov 15 '11 at 9:47
I had trouble using this in Rails 3.1 until I changed the name to url_for_image. I am not sure, but I think the method image_url was being looked for as a route. –  Luke Francl Dec 8 '11 at 17:33
I also had to change image_url method name to get_image_url to get this to work. –  tdewell Jan 22 '13 at 17:55
root_url broke if returns localhost:3000/?locale=en –  Evan Lee Dec 2 '13 at 1:25

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]
share|improve this answer
this answer is slightly different if using turbo-sprockets, btw. –  courtsimas Feb 22 '13 at 22:45
up vote 4 down vote accepted

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.

share|improve this answer
Any way to do this outside of Sass? image_url doesn't work in views. –  Luke Francl Dec 8 '11 at 0:14
I'm not entirely –  Steven Xu Dec 8 '11 at 4:30
yep. here's where i've used it before (an inline style): "background:url(#{asset_path('desktop-header@2x.jpg')}) top center no-repeat;" –  courtsimas Feb 22 '13 at 22:46

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)
share|improve this answer

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.


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);
share|improve this answer

Your Answer


