title: How do I include custom javascript on/for a single page in Rails?
body: <p>I have an interactive header for my site located in /views/layouts/_header.html.haml.</p>
  
  <p>I want all the JavaScript for the header to be collected into a single file. Most importantly, I'm using Twitter Typeahead and Bloodhound for a search field with auto-suggest. A lot of this JavaScript has to be run after the header is rendered, so its inclusion in application.js (which is included on my page in application.html.haml) doesn't work, as this runs before the header is rendered.</p>
  
  <p>I added search-bar.js under /assets/javascripts/ which contains all the JS I need to run on this page.</p>
  
  <p>At the bottom of my _header.html.haml I just linked with a regular script tag.</p>
  
  <pre><code>%script{src:"/assets/movie-search-bar.js", type:'text/javascript'}
  </code></pre>
  
  <p>This works fine locally, but on my dev server I get a 404 for that asset. Is it possibly throwing out the static file for performance reasons? Even if it does work, by having it included in the asset pipeline, wouldn't the script be loaded twice (once as an individual script, and once in application.js)? </p>
  
  <p>I'm getting the impression that this is not the best way to isolate the javascript of a partial into its own file. What is the best and most "railsy" way to ensure that the script is loaded once, after _header is rendered, and isolated within its own JavaScript file?</p>
  
  <p>EDIT: Could this perhaps be as simple as moving the following lines to the bottom of my footer partial? Is this considered good practice in Rails?</p>
  
  <pre><code>#{ stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' =&gt; true }
  #{ javascript_include_tag 'application', 'data-turbolinks-track' =&gt; true }
  </code></pre>