title: Using Rails 6, where do you put your “page specific” JavaScript code?
body: <p>My question here is the same as <a href="https://stackoverflow.com/questions/6167805/using-rails-3-1-where-do-you-put-your-page-specific-javascript-code">Using Rails 3.1, where do you put your &quot;page specific&quot; JavaScript code?</a>, just for Rails 6 instead of Rails 3.1.</p>
  
  <p>Suppose I have some JavaScript that I want to use for my posts index page. Where do I put that file, and how do I include it?</p>
  
  <p>In the previous question the answers usually utilize the Rails Asset Pipeline. However, with Rails 6, my understanding is that it uses webpacker instead of the Asset Pipeline for JavaScript files.</p>
  
  <p>Note: I don't want the file to <em>always</em> be included. Eg. if I am on the authors index page, I don't want the JavaScript file for the posts index page to be included. Why? Imagine I have <code>$('li').on('click', changeColor);</code> in the JS file for the posts index page. I probably don't want that code to run on the authors index page, but it will if the file is included. You could get around this problem by namespacing, but I think it would be cleaner (and more performant) to just not include unnecessary files.</p>