Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda