views:

305

answers:

2

Hi,

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.

+1  A: 

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
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
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
Did you try out my second suggestion?
Veeti
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
I got the impression that asset_host would add the specified URL to what javascript_path returns.
Veeti
In fact, it does - just add an asset_host and use javascript_path normally.
Veeti
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
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
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
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
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
+1  A: 

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.

Mark Quezada