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

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.