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

101
Vistas
What is the proper way to call javascript functions from within a rails view?

I'm really new to rails and the asset pipeline has been tripping me up for a bit. From what I understand, all javascript files in the assets/javascripts directory are compiled into one singular js file called application.js that contains all the functions in all the js files in the directory. My first question is, what happens if there's a naming conflict and two files in assets/javascripts have functions with the same name?

Secondly, what's the best practice for calling these functions? For example, in my view, I have a modal with a form that when submitted, I want the modal to disappear. I can do this

app/views/my_directory/index.html.haml

= form_with url: my_directory_path, class: "form", :html => { onsubmit: "hideModal()" },  method: :get do |form|
            = form.date_select
            %br
            %button.btn.btn-primary{type: "submit", class: "submitBtn"} Submit

app/assets/javascripts/modal.js

function hideModal() {
    $modal.modal('hide');
}

And that works fine. But from what I've read, it's not good practice to call javascript from within your rails view. So my second question is, what IS good practice? I've tried to delete the onsubmit option from my form and do this instead

modal.js

$('.form').submit(function() {
$modal.modal('hide');
})

but this doesn't work. I assume because it can't find the form, because there's no script tag with the javascript file within the index.html.haml. My guess is I'm misunderstanding how the asset pipeline works and how it "links" your javascript files and views together.

I'm working with Rails 5.2.

about 4 years ago · Juan Pablo Isaza
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