Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
JavaScript method isn't working on view in Rails 6+

I want to declare a method in a JS file and use it in the .html.erb like this below method

const getSetHtml = () => {
    console.log("Hello World");
}

and in the view .html.erb like this below code

onclick="getSetHtml()"

but it's showing

Uncaught ReferenceError: getSetHtml is not defined

I imported files like below

// /app/javascript/application.js
import "@hotwired/turbo-rails";
import "controllers";
import "trix";
import "@rails/actiontext";
import "trix";
import "@rails/actiontext";
import "./bootstrap.bundle.min";
import "./editor"; // this file

Note: Others JS library is working fine but the custom method isn't working.

Thanks

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Why do you want to set the function with const? If it fits with your specific use case, cool! Otherwise I would personally just go ahead and define it regularly, either way here is the syntax for what you're trying to do.

const getSetHtml = function() {console.log("Hello World")};

Your current function returns the function itself rather than returning the return value of the function(the console.log("Hello World")).

I can't be entirely sure but I believe in your application.js you can just import the file like this I would try the other thing first: import "editor";

I hope this helps even a little, good luck. Let me know how it goes!

about 4 years ago · Juan Pablo Isaza Report

0

You can expose your function by adding it to the global object

global.getSetHtml = () => {
  console.log("Hello World");
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!