Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 - How do you include external javascript libraries in vendor/assets/javascript?

In mulitple views I am including an external javascript library with this HTML:

<script type="text/javascript" 
        src="http://static.jstree.com/v.1.0pre/jquery.jstree.js">
</script>

In reading through the Rails 3.1 Asset Pipeline documentation and discussions I get the impression that vendor/assets/javascripts is a place were that file should be referenced. I'm guessing that I could download a copy of the file (jstree.js) and place it in that directory. However I'd like to have it loaded off the project site instead of making a local copy of it.

What do I put in vendor/assets/javascripts to pull a copy of jstree.js off the remote server? Do I create a .js file with some sort of remote load code? There seems to be all sorts of approaches and/or confusion on how to best do this ( see the long list of answers to this question: How do I include a JavaScript file in another JavaScript file? .)

Is there a "Rails Standard" convention/library/process for doing this? I'm a javascript neophyte, so please be explicit, thanks.

like image 228
Don Leatham Avatar asked Mar 30 '12 07:03

Don Leatham


People also ask

How to include js files in Rails?

Copy external JavaScript libraries (such as jQuery plugins) to the vendor/assets/javascripts folder. Let the Rails asset pipeline combine them all in one minimized application. js file. List scripts in the app/assets/javascripts/application.

How can we add an external js library to a webpage?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

What is an external JavaScript library?

All JavaScript libraries consists of two parts: The external JavaScript itself, which is simply a text file with the containing JavaScript code, saved as a . js file. A <script> tag referencing the external JavaScript file and defined on the page(s) that uses the library.


1 Answers

No, there's no way you can put a "reference" to a remote file in the assets folder. You either download a copy and put into assets, or reference remote file with <script> tag.

Having a copy is not a bad idea, really. At least, you know that it works and the file hasn't changed (unless you changed it yourself). When you load a remote file, all sorts of surprises can happen :)

like image 190
Sergio Tulentsev Avatar answered Sep 22 '22 04:09

Sergio Tulentsev