31

I'm using Rails 3.1. I'm trying to figure this out, and to my surprise, it is starting to seem that rails does not come with this method at all. Maybe im wrong.

Can anyone show how I can get a full absolute URL to an image?

I use asset_path(image.png) which gives me the relative path to use within the app. I tried doing a root_url + asset_path(image.png) but that just gives me a http://localhost:3000//assets/image.png with the double slashes

Anyone have an efficient way of doing this?

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
alik
  • 3,820
  • 9
  • 41
  • 55

7 Answers7

24

See the Using asset hosts section in the documentation. You need to specify an asset_host. You can also construct it dynamically from the request chaining "#{request.protocol}#{request.host_with_port}"

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • 1
    it seems like assets_host applies to all calls of asset_path, i just need an absolute url to an image once. I will look in to the request chaining – alik Sep 29 '11 at 14:19
  • 16
    Use `"#{request.protocol}#{request.host_with_port}#{asset_path("image.png")}"` – Simone Carletti Sep 29 '11 at 14:30
  • Be careful when using `#{request.protocol}#{request.host_with_port}#{asset_path("image.png")}`. If you set `config.action_controller.asset_host` option in your application configuration, then asset_path will return absolute url and you'll get something like `http://example.comhttp://cdn.example.com/assets/image.png`. – Vasily Reys Jul 16 '14 at 13:04
  • 1
    Simone, please mention that asset_url is not present in Rails 3! I just wasted 10 minutes researching it. – hakunin Jan 14 '15 at 10:17
  • 1
    It should be in Rails 3, this question was referencing Rails 3.1. – Simone Carletti Jan 14 '15 at 10:53
24

put this in application_helper.rb

def asset_url asset
  "#{request.protocol}#{request.host_with_port}#{asset_path(asset)}"
end

then you can use asset_url in your views.

greggreg
  • 11,945
  • 6
  • 37
  • 52
12

For Rails 4, and maybe earlier, use:

config.action_mailer.asset_host = 'https://assets.com'

per https://github.com/fphilipe/premailer-rails/issues/16

Luke W
  • 8,276
  • 5
  • 44
  • 36
6

In my config/environments/*.rb I already have this tailored for each environment:

config.domain = 'mysite.dev'

So it was a simple matter of adding

config.action_controller.asset_host = "http://" + config.domain

to each file. Then asset_path will miraculously behave as if it were asset_url.

Prathan Thananart
  • 4,007
  • 3
  • 19
  • 18
  • You shouldn't even need to put the protocol in there, just configuring `asset_host` should make `asset_path` add the protocol and hostname prefix. – smathy Feb 22 '13 at 00:12
  • 2
    I find this rails behaviour wrong, `asset_path` should be a path `asset_url` should be an url. I really don't like the way rails change the asset_path behaviour if there's a `asset_host` =/ – Tiago Apr 24 '14 at 22:24
0

Example folder structure.

app/
   assets/
      flags/
         32x32/
            en.png
         256x256/
            en.png

If you want to generate absolute flag image path we can add in to our ApplicationHelper two methods:

module ApplicationHelper

  # Generate flag path by locale
  # - locale. Can be "en", "it", etc.
  # - flag_size. Will be used to set folder size. Folder size can be "32x32", "256x256".
  # Return flag image path. Path will absolute
  def generate_flag_path_by_locale(locale, folder_size = "32")
    folder = "#{flag_size}x#{flag_size}"
    domain_absolute_path = generate_domain_absolute_path
    flag_path = ("#{domain_absolute_path}/assets/flags/#{folder}/#{locale}.png")

    return flag_path
  end

  # Generate domain absolute path
  def generate_domain_absolute_path
    request_protocol = request.protocol
    request_host_with_port = request.host_with_port
    domain_absolute_path = request_protocol + request_host_with_port

    return domain_absolute_path
  end
end

Into our apps/view/products.html.erb. We must to call only:

<% flag_path = generate_flag_path_by_locale("en") %> 

Final result:

http://my_domain.com:3000/assets/flags/32x32/en.png

d.danailov
  • 9,594
  • 4
  • 51
  • 36
-1

Could you just do:

root_url[0..-2] + asset_path(image.png)

...to trim the trailing slash in the root url?

Chris Staikos
  • 1,150
  • 10
  • 24
-10

You need to use 'asset_url' instead *asset_path*.

Bcz '_path' always return relative path and '_url' will return absolute url.

HiteshRawal
  • 445
  • 2
  • 10
  • 3
    already tried that. there is no such method as asset_url. here's what I get `undefined method `asset_url'` – alik Sep 29 '11 at 13:12
  • please try this ActionController::Base.asset_host = "assets.example.com" or check the link http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html – HiteshRawal Sep 29 '11 at 13:54
  • it seems like assets_host applies to all calls of asset_path, i just need an absolute url to an image once. – alik Sep 29 '11 at 14:20
  • If you want only once, then in such case you can use your own customize helper method. – HiteshRawal Sep 29 '11 at 14:28
  • `asset_url` doesn't exist in CSS like the _path vs. _url naming convention does elsewhere in helpers. The CSS helper titled `asset-url` (notice the dash instead of underscore) doesn't work in the same convention, but instead just attaches a `url('image.png')` framework to the CSS data. – toobulkeh Apr 02 '14 at 01:57