Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 600
Steven Avatar asked Jun 27 '11 23:06

Steven


People also ask

How can you get the current absolute URL in your Ruby on Rails view?

You should use request. original_url to get the current URL.

What is assets in Ruby on Rails?

The lib/assets (or sometimes vendor/assets ) is where you supposed to place js/css/images related to your front-end design and provided by third party libs. Say if you want to pull in some css or a js framework, that were you should place it.


1 Answers

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.

like image 134
tig Avatar answered Sep 18 '22 17:09

tig