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

Viewed 31911

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.

7 Answers

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']}/")
Related