Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda