I'm trying to implement some JavaScript in my Rails 6 app, I have no idea if I'm going about it the right way but so far everything I've tried has been wrong.
I have a file javascripts/form_helpers/submit.js
for now it just looks like this:
const submit_action() {
console.log("hello world");
}
In javascripts/application.js
I have
require("form_helpers");
# I've also tried require("form_helpers/submit.js") but I thought I could just include the directory which seems to be what Rails ships with for channels
Then in the actual view I have a form_with
tag:
<%= submit_tag 'Submit', class: "button button-create button-log-radius", onclick: "submit_action()" %>
I always get a reference error when I click the submit button that submit_action is not defined. Am I on the right track here? Am I way off base? I'm a little confused by this new javascript folder, am I using it right or should I be putting scripts like this into the assets directory?
BTW I've seen this question asked before on here and the answer had to do with appending window
before your function name (here) but that feels hacky to me.