Have you found this?
Where to Stick Your JavaScript.
Where to Stick Your JavaScript
Whether you use the Rails asset pipeline or add a <script>
tag directly to a view, you have to make a choice about where to put any local JavaScript file.
We have a choice of three locations for a local JavaScript file:
- the
app/assets/javascripts
folder
- the
lib/assets/javascripts
folder
- the
vendor/assets/javascripts
folder
Here are guidelines for selecting a location for your scripts:
- Use
app/assets/javascripts
for JavaScript you create for your application.
- Use
lib/assets/javascripts
for scripts that are shared by many applications (but use a gem if you can).
- Use
vendor/assets/javascripts
for copies of jQuery plugins, etc., from other developers.
In the simplest case, when all your JavaScript files are in the app/assets/javascripts
folder, there’s nothing more you need to do.
Add JavaScript files anywhere else and you will need to understand how to modify a manifest file.
Mysterious Manifests
There are two kinds of files in a JavaScript assets folder:
- ordinary JavaScript files
- manifest files
You can also have CoffeeScript
files and ERB
files which are variations on ordinary JavaScript files.
Manifest files have the same .js file extension as ordinary JavaScript files. Manifest files and ordinary JavaScript files can be combined in a single file. This makes manifest files mysterious, or at least non-obvious.
The default app/assets/javascripts/application.js file is a manifest file
. It’s a manifest file because it contains directives:
//= require jquery
//= require jquery_ujs
//= require_tree .
Directives tell Sprockets which files should be combined to build a single JavaScript script. Each file that contains manifest directives becomes a single JavaScript script with the same name as the original manifest file. Thus the app/assets/javascripts/application.js
manifest file becomes the application.js
script.
All scripts in the app/assets/javascripts
folder are automatically added to the default application.js
script when the manifest file includes the default //= require_tree . directive.
Hope this helps.