Let’s say you have a form that a user fills out and you want to do form validation. If the user submits the form without filling in the proper values, the page for the form has to reload again, hitting the server and coming up again for the user. That takes a lot of time and will probably annoy the user.
Of course you can sometimes get away with HTML form validation, but that’s not always possible. Adding a simple script that checks the user inputs and notifies them that they should input the correct information is much better.
Of course this doesn’t mean you can ignore server side validation, but that’s for another article.
But we love Rails, so we are going to stick with it for a while.
What we’ll cover here
In that folder you will find the application.js file, which is just like the application.css file. It will be imported by default in the application.html.erb file when you create your new Rails app.
The application.html.erb file will be used by all views.
Adding a script that will be used by all views
The first four lines are there by default. I have added a
You can test this by viewing any page in your application and opening the developer console.
Adding a script that will be used by a specific file
I hope this article helps clear up any confusion you might have when updating your app to Rails 6, or if you just got started with Rails.
You can follow me on Github if you want to learn more.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. <a href="https://www.freecodecamp.org/learn">Get started</a>