Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate full url to javascript in rails (similar to javascript_path, but url)

How is it possible to generate an absolute link to the javascript file.

I assume there should be something like the one below (which unfortunately does not seem to be available):

javascript_url 'main' # -> 'http://localhost:3000/javascripts/main.js'

instead of:

javascript_path 'main' # -> '/javascripts/main.js'

I need the absolute URL because that javascript file will be used in a bookmarklet.
Additionally I need the same for css file.

Thanks,
Dmitriy.

like image 244
Dmytrii Nagirniak Avatar asked Dec 21 '09 12:12

Dmytrii Nagirniak


1 Answers

Absolute URLs to javascript and css files are now available in Rails 4 in ActionView::Helpers::AssetUrlHelper module via Asset Pipeline. In your case specifically it's javascript_url and its alias url_to_javascript and corresponding methods for stylesheets.

And the result is exactly as you mentioned in your question:

javascript_url 'main' # -> 'http://localhost:3000/javascripts/main.js'

Though the question was asked ages ago, hope it will help people seeking answer to the same question in future.

like image 73
Zeeshan Avatar answered Sep 17 '22 18:09

Zeeshan