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

helper, ruby, ruby-on-rails, url

Solution

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.

Problem

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.

Original source