6

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.

Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130

3 Answers3

7

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.

Zeeshan
  • 3,462
  • 2
  • 25
  • 28
2

I've written this little helper to do this:

def absolute_javascript_url(source)
  uri = URI.parse(root_url)
  uri.merge(javascript_path(source))
end

The key part being URI.merge which will automatically, and correctly merge the relative javascript_path with root_url.

markquezada
  • 8,444
  • 6
  • 45
  • 52
1

Maybe you could simply use javascript_path combined with root_url?

For example:

root_url + javascript_path("main") 

root_url is automatically generated by your root route.

You could also configure the Rails helpers to use a specific "base path" by setting ActionController::Base.asset_host in your environment's configuration file. Read more in the documentation.

Veeti
  • 5,270
  • 3
  • 31
  • 37
  • root_url ends with slash, havascript_path starts with slash. So I end up with double slashes in the path. Much better way of doing it should exist. – Dmytrii Nagirniak Dec 21 '09 at 13:09
  • Another why root_url is bad is that I won't be able to move the assets to a different server as toot_url alwasy points the host of current request. – Dmytrii Nagirniak Dec 21 '09 at 13:13
  • Did you try out my second suggestion? – Veeti Dec 21 '09 at 13:15
  • asset_host only affect the assets. It doesn't affect routed url (root_url is generated by routing module). So not sure what exactly you suggest. – Dmytrii Nagirniak Dec 21 '09 at 13:48
  • I got the impression that asset_host would add the specified URL to what javascript_path returns. – Veeti Dec 21 '09 at 14:25
  • In fact, it does - just add an asset_host and use javascript_path normally. – Veeti Dec 21 '09 at 14:27
  • Ohh. Ok. So are saying to add localhost to the asset_host. It should work, but in this case I will have ALL assets with absolute URL. This is not what I want. I need only on :( – Dmytrii Nagirniak Dec 21 '09 at 15:02
  • Well, the only solution I can think of would be to store the base path in a YAML configuration file ( check http://railscasts.com/episodes/85-yaml-configuration-file ) and then manually read it every time you want to output an absolute link. Of course, hardcoding it would also work, but YAML is probably a better choice... – Veeti Dec 21 '09 at 15:15
  • Why should I put it into config if the value is know at runtime with no configuration at all? Isn't there really any other way except ridiculous `root_url.chop! + javascript_path("main")`?? – Dmytrii Nagirniak Dec 21 '09 at 15:33
  • I suggested YAML configuration so that you wouldn't need to hardcode any URL's into your views. If you're waiting to solve this problem, you might just temporarily want to make an application-wide helper method that will return the JavaScript URL and modify that to use the final solution later. – Veeti Dec 21 '09 at 16:06
  • Well, this is what I am after - the final solution. I already use a temporary workaround. Just can't believe that rails does not provide such a simple thing. – Dmytrii Nagirniak Dec 21 '09 at 16:23