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.