Ruby on Rails

Latest Posts

Process slow network requests with Turbo and Active Model

Learn how to build a dynamic loading screen without writing a line of JavaScript.

Read More

Build a (better) search form in Rails with Active Model

Harness the power of Active Model to supercharge your search forms.

Read More

Conditionally render a Turbo Frame shared between multiple views

We explore several solutions to a common Hotwire problem.

Read More

A pragmatic guide for adding React to an existing Rails application (and still use Hotwire)

For a long time, I thought that using React with Rails was an "all-or-nothing" proposition. I'm now realizing that the two can be integrated on a spectrum.

Read More

Configure GitHub Actions to work with rspec-rails

The next release of Rails will ship with a CI template that will "work out of the box"... unless you're using RSpec.

Read More

Rails advanced routing constraints

Learn how to authorize requests at the routing layer to improve security and ergonomics.

Read More

Are your polymorphic relationships correctly enforced?

My client project uses a polymorphic relationship between several models in an effort to create a flexible system of associations.

Read More

Database View Backed Scopes In Rails

How do you manage queries when your Rails application isn't the only thing reading your database?

Read More

Are you absolutely sure you know how to use the button element?

Read this the next time you need to build a complicated form.

Read More

Are you absolutely sure your Rails caching strategy isn't leaking sensitive information?

Rails writes a new cache entry based on the first request. But what happens when that request is from an admin?

Read More

Building Value Objects in Rails with composed_of

Learn how to improve the interface of your existing Active Record Models with this underutilized API.

Read More

Are you absolutely sure your `has_one` association really has one association?

Learn about an unexpected limitation with this API and how to combat it universally.

Read More

How to click links in Rails Mailers when writing tests

When writing an integration or system test in Rails, have you ever needed to click a link in an email? This is especially important for testing links with a unique token or that expire, such as password reset emails. Simply checking that the link exists is not sufficient, since the page it leads to is ephemeral.

Read More

Your flaky tests might be time dependent

Time dependent tests are difficult to diagnose, and the symptoms don’t present themselves until it’s too late. This article will help you determine if your test suite is time dependent.

Read More

Detect anomalies in user behavior using Rails and PostgreSQL

You probably use some type of error monitoring software to notify you when an exception is raised in your application, but are you being notified when there is an anomaly in user behavior?

Read More

Query by Duration in Active Record

How do you even save a “duration”, let alone query for records by that value? It’s actually easier than you think.

Read More

Rails Server Side Analytics From Scratch

Learn how to track user events without sacrificing privacy and performance.

Read More

Rails Setup Script Improvements

Rails ships with a setup script that automates bootstrapping a new application, but did you know that you’re free to edit this script? In this quick tutorial, I’ll show you how you can improve the default Rails setup script to create a more consistent and helpful onboarding experience for folks on your team.

Read More

Hotwire: Asynchronously loaded tooltips

Learn how to effectively render tooltips in Rails without writing any JavaScript.

Read More

Rails Authentication From Scratch

If you’re like me then you probably take Devise for granted because you’re too intimidated to roll your own authentication system. As powerful as Devise is, it’s not perfect. There are plenty of cases where I’ve reached for it only to end up constrained by its features and design, and wished I could customize it exactly to my liking.

Read More

Rails Remote Elements Tutorial

Do you need to create real-time features in your Rails app, but either can’t use Turbo or don’t want to use a front end framework like React? Fortunately older versions of Rails actually provide this functionality of the box. In this tutorial I’ll show you how to create a single page app in Rails from scratch using remote elements and Stimulus.

Read More

Lazy Load Content in Rails from Scratch

Are certain pages on your Rails app loading slowly? You might want to consider loading those requests in the background. It’s easier than you think. In this tutorial I’ll show you how to lazy load content in Rails without Hotwire.

Read More

Automatically Unsubscribe from Emails in Rails (and Control Email Preferences)

In this tutorial, I’ll show you how to add a link to any Rails Mailer that will allow a user to automatically unsubscribe from that email. As an added bonus, we’ll build a page allowing a user to update their email preferences across all mailers.

Read More

Build an API in Rails with Authentication

In this tutorial, we’ll build a full-featured API in Rails with authentication. Below is what we’ll cover.

Read More

Search Across Multiple Models in Rails

In this tutorial you’ll learn how to search across multiple models in Rails. Below is a demo of what we’ll be building. Note how both Post and User records appear in the search. As an added bonus, we highlight the search query in the results.

Read More

Use Pundit as a Rails Feature Flag System

In this tutorial, I’ll show you how to create a feature flag system in Rails using pundit and a features column on the users table.

Read More

Real-time Form Validation in Ruby on Rails

Learn how to validate a form in real-time while conditionally preventing it from being submitted.

Read More

Create an infinite scrolling blog roll in Rails with Hotwire

In this tutorial, I’ll show you how to add an infinitely scrolling blog roll using Rails and Hotwire. Note that this is different than Chris Oliver’s awesome infinite scroll tutorial, in that we’re loading a new post once a user scrolls to the bottom of a current post. Below is a demo.

Read More

Generate Link Previews in Rails with Microlink

In this tutorial I’ll show you how to leverage the Microlink API to generate link previews in Ruby on Rails.

Read More

Auto Save Form Data in Rails

In this tutorial I’m going to show you how to automatically save form data in Rails. Instead of saving a draft to the database, we’ll simply leverage Stimulus JS to save the data to localStorage. Below is what we’ll be creating.

Read More

Easily find elements with XPath in Capybara by using Chrome's Dev Tools

Imagine you have multiple identical elements on a page and that you need to select a specific element during a system test.

Read More

Install Bootstrap 4 on a Rails 6 App with Webpacker

I know many existing tutorials explain how to install Bootstrap 4 on a Rails 6 application with Webpacker (like the GoRails example). However, I wanted to create this tutorial to highlight a few gotchas, as well as highlight why and how I made some of my choices.

Read More

Configure Travis CI for Ruby on Rails

In this tutorial I am going to show you how to configure Travis CI to run your Rails’ test suite and system tests everytime you push a new change to your repository.

Read More

PaperTrail Gem Tutorial

In this tutorial I am going to show you how to revert and restore records using the PaperTrail Gem.

Read More

Obfuscate Numerical IDs in Rails

By default, Rails displays the record’s ID in the URL (e.g. http://localhost:3000/articles/1). Although there is nothing inherently wrong with this approach, sometimes it’s helpful to obfuscate the record’s ID (e.g. http://localhost:3000/articles/xb3mm6k). In this tutorial I will show you how to obfuscate numerical IDs in Rails.

Read More

Ruby on Rails with React Tutorial

Many Rails+React tutorials demonstrate how to create an API only application using Rails, and then create a separate front-end application to digest the API with React. Other React tutorials have you work with something like Firebase to handle the back-end of the application. Although both of these approaches are common and acceptable, I wanted to create an application that has all the benefits of a non API only Rails application, without the limitations and vendor lock-in of a third party service like Firebase.

Read More

Create a Nested Form in Rails from Scratch

The Rails Guides give a great example of how to create nested forms. However, Rails does not support adding fields on the fly out of the box. The cocoon Gem is one alternative, as is Ryan Bates’ excellent tutorial. However, both require jQuery which does not ship with Rails 6.

Read More

Rails Ajax Examples (Without jQuery)

Rails ships with turbolinks which creates a reactive, fast application. However, there are times when turbolinks is not enough, and you’ll want to roll your own AJAX solutions.

Read More

Why I Think Rails is Still Relevant in 2019

There is currently a high demand for Javascript developers, which in turn dictates the content and curriculum of many tutorials, articles and Boot Camps. Therefore, if you’re new to web development or maybe strictly just a front end web developer, you probably don’t hear a lot about Ruby on Rails these days. I’m here to make a case that Rails is still a very relevant tool in 2019, and should not be overlooked when starting a new project.

Read More

Create Dependent Associations in FactoryBot

Imagine the following set of models and relationships: A user can add a time_entry to a job. The time_entry has a task, and that task has a rate which depends upon the job. So I need to validate that the associated time_entry on a job is associated with a rate that is also associated with that job. Basically, I want to make sure the correct rate is being applied to the job.

Read More

Use a Custom Column Name With a belongs_to Association in Rails

There may be times when you want to customize the column name to be more expressive.

Read More

Configure RSpec and Capybara with Ruby on Rails

In this tutorial I’m going to show you how to configure your Rails app to use RSpec and Capaybara. In addition, we will also install and configure Factory Bot Rails and Database Cleaner, since these Gems help with a better testing experience.

Read More

Convert a belongs_to Association to a has_many :through Association in Ruby on Rails

I was tasked with converting a belongs_to association to a has_many :through association. The challenge was that the app was live, and there were existing relationships. I was able to convert the belongs_to association to a has_many :through association while still maintaining existing relationships by creating a custom migration file, and updating the existing models.

Read More