In this tutorial, we aim to guide you on how to incorporate JavaScript and CSS into your Rails views. This integration will allow you to create more dynamic, responsive, and visually engaging web applications.
By the end of this tutorial, you will learn:
Prerequisites:
Rails utilizes the asset pipeline for managing and organizing JavaScript, CSS, and image files. The asset pipeline concatenates and minifies the files, reducing the number of requests and increasing loading speed.
CSS files in Rails are typically stored in the app/assets/stylesheets
directory. You can create separate CSS files for different views or components and include them in your layout file.
Example:
app/assets/stylesheets
named styles.css
/* app/assets/stylesheets/styles.css */
body {
background-color: lightblue;
}
<!-- app/views/layouts/application.html.erb -->
<%= stylesheet_link_tag 'styles', media: 'all', 'data-turbolinks-track': 'reload' %>
JavaScript files are stored in the app/assets/javascripts
directory. Similarly to CSS, you can create separate JavaScript files and include them in your layout file.
Example:
app/assets/javascripts
named scripts.js
// app/assets/javascripts/scripts.js
document.addEventListener('DOMContentLoaded', function() {
console.log('Page is loaded');
});
<!-- app/views/layouts/application.html.erb -->
<%= javascript_include_tag 'scripts', 'data-turbolinks-track': 'reload' %>
Let's look at some practical examples.
button.css
/* app/assets/stylesheets/button.css */
.btn {
background-color: blue;
color: white;
}
button.css
in your layout file<!-- app/views/layouts/application.html.erb -->
<%= stylesheet_link_tag 'button', media: 'all', 'data-turbolinks-track': 'reload' %>
<!-- app/views/examples/show.html.erb -->
<button class="btn">Click me</button>
button.js
// app/assets/javascripts/button.js
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('.btn').addEventListener('click', function() {
alert('Button clicked');
});
});
button.js
in your layout file<!-- app/views/layouts/application.html.erb -->
<%= javascript_include_tag 'button', 'data-turbolinks-track': 'reload' %>
In this tutorial, we've covered how to add JavaScript and CSS to your Rails views. We've learned how to create separate files for your JavaScript and CSS, and how to include them in your layout file.
Next, you might want to learn more about the Rails asset pipeline, which provides features like asset minification and compression.
Create a CSS file that styles a paragraph with a custom font color and size. Include it in your Rails view and confirm that the styles are applied.
Create a JavaScript file that logs a custom message to the console when a button is clicked. Include it in your Rails view and confirm that the message is logged when the button is clicked.
Create a CSS file that styles a div with a custom background color. Then, create a JavaScript file that changes the div's background color when it's clicked. Include both files in your Rails view and confirm that the div's background color changes when it's clicked.
Remember, practice makes perfect. Keep experimenting with different CSS styles and JavaScript functionalities to get a better understanding of how they can enhance your Rails views.