I want to use view specific JavaScript in Ruby on Rails 4.2.0 so I wanted to load it dynamically wherever I want to load it. I put the the javascript file which should be loaded(headers.js) in assets/javascripts. I removed the require tree line in the application.js file.
I have updated application.html.erb
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Sandbox</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag params[:controller] %>
<%= csrf_meta_tags %>
<%= render 'partials/shim' %>
</head>
<body>
<%= render 'partials/header' %>
<%= yield :javascript_includes %>
<%= render 'partials/footer' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
</body>
</html>
and I wanted to call the javascript file on the very top of the view.
view.html.erb
<% content_for :javascript_includes do %>
<%= javascript_include_tag "headers.js" %>
<% end %>
I have updated the initializers/assets.rb file as well:
Rails.application.config.assets.precompile += %w( headers.js )
For some reason the html content of the view will not load and I'm getting an error in the console: Uncaught ReferenceError: jQuery is not defined.
Why it does not work? How I can make this work and what am I missing?
<%= javascript_include_tag "headers.js" %>and restart your server, what do you get?