Rails’ modest JavaScript framework is a great addition to its ecosystem

Stimulus, as indicated on the website https://stimulusjs.org/, is a modest JavaScript framework that is simple to understand and use, but it still brings powerful abstractions to enable advanced use.

In this tutorial, we’ll dive into some practical examples of how to use Stimulus. If you want to follow along, you can start from this tutorial’s repository base branch: https://github.com/rodloboz/stimulus-rails/tree/base

Installing Stimulus

We’re using Rails 6 which ships with Webpack by default. In the terminal, let’s import Stimulus into our project with yarn add stimulus (or npm). Then we’ll add the default Stimulus boilerplate in app/javascript/vendor/stimulus.js and require this file in index.js …


Rails’ modest JavaScript framework is a great addition to its ecosystem

Image for post
Image for post
Photo by Marius Masalar on Unsplash

You can see the finished code here: https://github.com/rodloboz/stimulus-rails or visit the Heroku app here: https://stimulus-rails-tutorial.herokuapp.com/

Stimulus, as indicated on the website https://stimulusjs.org/, is a modest JavaScript framework that is simple to understand and use, but it still brings powerful abstractions to enable advanced use.

In this tutorial, we’ll dive into some practical examples of how to use Stimulus. If you want to follow along, you can start from this tutorial’s repository base branch: https://github.com/rodloboz/stimulus-rails/tree/base

Installing Stimulus

We’re using Rails 6 which ships with Webpack by default. In the terminal, let’s import Stimulus into our project with yarn add stimulus(or npm). Then we’ll add the default Stimulus boilerplate in app/javascript/vendor/stimulus.js and require this file in index.js


A quick overview of ActiveRecord polymorphic associations.

Image for post
Image for post
Photo by Cécile Brasseur on Unsplash

When developing our applications, we often encounter situations where two or more different models have associations that seem to be identical. For instance, we may have a Product and a Service model that have many photos, or a Project, a Contractor, and a Task model that have many comments. One way to approach this problem would be to use one child database table for each parent model:

class Product < ApplicationRecord
has_many :photos, class_name: 'ProductPhotos'
end
class Service < ApplicationRecord
has_many :photos, class_name: 'ServicePhotos'
end
class Project < ApplicationRecord
has_many :comments, class_name: 'ProjectComments'…


How to quickly set up two different types of users with Rails and Devise.

Image for post
Image for post
Photo by Katka Pavlickova on Unsplash

Sometimes we need to have different types of Users in our app. Of course, we could generate two different Devise models and have distinct signup and login flows, but there’s more than one way of achieving a similar result. In this tutorial, we’ll explore Rails’ Single Table Inheritance (STI) on a User devise model.

Let’s say that we have an app where the User model has the email, password, and other Devise related attributes, plus a first_name and last_name. Now, let’s imagine that there is one type of User we’ll call Customer and another one named Seller. For the purpose of this example, Customers can sign up for the application whereas Sellers are created by the application owners and are not able to directly sign up on the web app. …


In this multi-part tutorial, we will be building a Ruby on Rails app that implements the look and the features of popular social media giant Instagram. We’ll use Rails 6, Webpacker 4, Turbolinks, Stimulus Js and Tailwindcss.

Image for post
Image for post

DISCLAIMER: While the purpose of this tutorial is to share knowledge and show how to implement certain features for learning, due to its length and text format, it’s not aimed at the novice Rails developer and it’s not an introduction to Rails. If you’re still unfamiliar with the structure of a Rails application, I recommend starting with some basic Rails tutorials first.

I’ve set up a Rails 6 app (rc1 version) with Webpacker 4, Turbolinks and the upcoming release 1.0 of Tailwindcss. If you want to follow along you can clone the app from the following…


Aggregate points on a map in clusters with Ruby on Rails and Mapbox

Image for post
Image for post

When we have a geocoded model in Rails, we may want to display the records in our database as markers on a map. However, once you begin to have too many records close to each other, it ruins the map experience for your users.

One solution to this problem is to aggregate markers that are close to each other in clusters which break apart as soon as the zooms in. Mapbox’s mapbox-gl API has this functionality and we’ll implement it within the context of Rails.

We’re going to be using Ruby on Rails 5 with Webpacker 4 and Turbolinks, but the setup should be similar with other versions. In our example, we have a Car model that is geocoded — it has latitude and longitude coordinates. …


Ruby and JavaScript are about to go head to head. Both of these scripting languages are dynamically typed and support object-oriented programming. We’ll examine their differences or similarities across some of their more common features.

Disclaimer: We’re only looking at modern JavaScript ES6 syntax here.

Image for post
Image for post
Photo by Pramote Polyamate on 500px

String interpolation

In computer programming, string interpolation is the process of injecting the value of a variable or an expression in a string literal.

In Ruby, this is called, you guessed it, string interpolation.

Ruby:

first_name = "Martin"
last_name = "Riggs"
puts "Hi, I'm #{first_name} #{last_name}."

In Javascript, the same thing can be achieved with template literals.

JavaScript…


In this article, I examine the rails routing system, including its basic syntax, standard CRUD routes, nested resources, member and collection routes, namespaced and scoped routes, and other advanced patterns.

Image for post
Image for post
Photo by Steve Harvey on Unsplash

I often see beginners not taking full advantage of the powerful routing engine provided by Ruby on Rails. Often they’re using more cumbersome syntax duplicating code or, worse, breaking Rails conventions left and right. But before we dive into concrete examples, it’s important to understand what the Rails routes actually are. According to Obie Fernandez in The Rails 5 Way:

The routing system in Rails is the system that examines the URL of an incoming request and determines what action should be taken by the…


Learning how to code nearly two years ago turned my life around and changed my career. Whether you’re thinking of joining a coding bootcamp, currently attending one or have recently graduated, here’s my advice for before, during and after.

Image for post
Image for post
Photo by Christopher Gower on Unsplash

Last Friday, we wrapped up Batch 205 of Le Wagon’s coding bootcamp here in Lisbon. That’s 205 courses run worldwide for Le Wagon and the 9th batch in Lisbon. That got me thinking about how much I’ve come along since graduating from batch 49, the second batch in Lisbon, nearly two years ago.

With an estimated information technologies jobs gap of 500,000 by 2020 in the European Union alone, bootcamps are rising as an alternative to fill in vacancies in the sector. And many people are changing their careers by enroling in coding bootcamps. With a background in literature and sales, I am one of those people. Since then, I have also helped others do the same as a teacher with Le Wagon. Here’s my advice on maximizing the bootcamp experience. …


Subjects covered in this article: Ruby on Rails custom validation rules, each validator, displaying a date-picker with flatpickr and writing a model instance method to list all unavailable dates to be passed into the date-picker.

Image for post
Image for post

For the past 2 years, I have been teaching web development to several batches of the Le Wagon coding bootcamp. We teach a full-stack curriculum and during the final two weeks the students work in teams to ship two different Rails apps to production, one of which is a two-sided marketplace concept, where there are users who put up something for rent or for sale, and users who book a product or a service (or make reservations). Because this project only lasts for one week, often we tell students to focus solely on one side of the story, and we ask them to assume, for the sake of simplicity, that the product/service is always available, regardless of whether or not there are bookings for selected dates. …

About

Rui Freitas

Lead Teacher @ Le Wagon | Web Developer @ Light the Fuse and Run: http://lightthefuse.run/ | Photographer @ Rod Loboz: https://blog.rodloboz.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store