本文介绍了生成Rails中javascript的完整URL(类似于javascript_path,但是url)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何生成指向JavaScript文件的绝对链接.

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'

代替:

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

我需要绝对URL,因为该JavaScript文件将用于书签.
另外,我需要使用相同的CSS文件.

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

谢谢,
德米特里(Dmitriy).

Thanks,
Dmitriy.

推荐答案

Rails 4中的 ActionView :: Helpers :: AssetUrlHelper 模块.在您的情况下,它是 javascript_url及其别名url_to_javascript 以及样式表的相应方法

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.

这篇关于生成Rails中javascript的完整URL(类似于javascript_path,但是url)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 08:42