0

In my rails app, in a javascript file in assets/javascripts, the first line is console.log("This javascript file is included"). In My application.html, I include this right after the head:

<script type = "text/javascript">
console.log("Logging from the application.html")
</script>

I don't explicitly include the javascript file in any of my views, yet the console prints:

This javascript file is included.
Logging from the application.html
Rose Perrone
  • 61,572
  • 58
  • 208
  • 243

3 Answers3

1

You're application.js probably looks something like:

//= require_tree .

"The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file. You can also use the require_directory directive which includes all JavaScript files only in the directory specified, without recursion."

http://guides.rubyonrails.org/asset_pipeline.html

Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130
0

It sounds like you have <% javascript_include_tag :all %> in one of your views or layouts, which causes all files in public/javascripts to be included. Documentation is here.

It's also possible that the file in question has been added to config.action_view.javascript_expansions[:defaults] in config/application.rb, which would cause it to be loaded if you have <% javascript_include_tag :defaults %> somewhere.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • I grepped my project folder and found no occurrence of the pattern "javascript" in any files (besides the development log and assets cache) that I hadn't written myself. – Rose Perrone May 23 '12 at 20:04
0

Your layout/application.html.erb is base to any your view (see yield instuction). At any view (any controller#method calling) rails render layout this current view file in yield. Obvious, js "Logging from the application.html" run at every refreshing page (not AJAX).

About including external JS, see previous post. If you wanna get page specific js read Best way to add page specific javascript in a Rails 3 app?

Community
  • 1
  • 1
dmr
  • 322
  • 1
  • 11