Adding JavaScript and CSS to Rails Views

Tutorial 4 of 5

1. Introduction

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:

  • How to include JavaScript and CSS in your Rails views
  • Best practices for organizing your JavaScript and CSS files in Rails
  • How to use Rails' asset pipeline for managing static files

Prerequisites:

  • Basic understanding of Ruby on Rails
  • Familiarity with HTML, CSS, and JavaScript

2. Step-by-Step Guide

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.

2.1 Adding CSS to Rails Views

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:

  1. Create a CSS file in app/assets/stylesheets named styles.css
/* app/assets/stylesheets/styles.css */

body {
  background-color: lightblue;
}
  1. Include the CSS file in your layout file
<!-- app/views/layouts/application.html.erb -->

<%= stylesheet_link_tag 'styles', media: 'all', 'data-turbolinks-track': 'reload' %>

2.2 Adding JavaScript to Rails Views

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:

  1. Create a JavaScript file in app/assets/javascripts named scripts.js
// app/assets/javascripts/scripts.js

document.addEventListener('DOMContentLoaded', function() {
  console.log('Page is loaded');
});
  1. Include the JavaScript file in your layout file
<!-- app/views/layouts/application.html.erb -->

<%= javascript_include_tag 'scripts', 'data-turbolinks-track': 'reload' %>

3. Code Examples

Let's look at some practical examples.

3.1 Styling a Button with CSS

  1. Create a CSS file button.css
/* app/assets/stylesheets/button.css */

.btn {
  background-color: blue;
  color: white;
}
  1. Include button.css in your layout file
<!-- app/views/layouts/application.html.erb -->

<%= stylesheet_link_tag 'button', media: 'all', 'data-turbolinks-track': 'reload' %>
  1. Use the CSS class in your view
<!-- app/views/examples/show.html.erb -->

<button class="btn">Click me</button>

3.2 Making a Button Interactive with JavaScript

  1. Create a JavaScript file button.js
// app/assets/javascripts/button.js

document.addEventListener('DOMContentLoaded', function() {
  document.querySelector('.btn').addEventListener('click', function() {
    alert('Button clicked');
  });
});
  1. Include button.js in your layout file
<!-- app/views/layouts/application.html.erb -->

<%= javascript_include_tag 'button', 'data-turbolinks-track': 'reload' %>

4. Summary

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.

5. Practice Exercises

  1. 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.

  2. 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.

  3. 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.