I want to have a JavaScript file available for a view in my Rails app.
// app/javascript/custom/manage.js
document.body.style.backgroundColor = 'lightblue'
const test = document.getElementById('test')
# config/importmap.rb
pin_all_from "app/javascript/custom", under: "custom"
# app/views/locations/manage.html.erb
<%= javascript_include_tag "custom/manage", defer: true %>
When I navigate back to the manage view, I am getting the following error:
Uncaught SyntaxError: redeclaration of const test
I suspect this is because of how Turbo works. Because I tried commenting it out in the application.js file and the error was gone.
Failed to register controller: hello (controllers/hello_controllerandUncaught DOMException: Node.removeChild: The node to be removed is not a child of this nodeon refresh).javascript_import_module_tagworks as it doesn't load the JavaScript on subsequent visits to the page - but that's a bug for me because I want the JS to be associated to with the view and run everytime I visit the view rendered bymanage.html.erb