Zackon Stack Overflow • Recommends
On “Where to place Javascript used in one page only in Rails 7 with importmap”
As @Azrklm points out, the answer is contained in this video link to this video beginning at time 21:38.
Suppose we have some JavaScript that we want to run only on a view called FancyView . Here are the steps
Place the code unique to FancyView in its own file, say FancyView.js (and remove it from application.js , if necessary).
Add a line for FancyView.js to the importmap: pin "FancyView"
Add the line <%= yield :head %> to application.html.erb`. (This tells Rails that individual views/layouts may want to add additional information to the web page's head.)
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %> <%# This loads application.js %>…