Using PurgeCSS with Rails

We leverage CSS frameworks like Bootstrap or Tailwind in our applications, but we likely won't use *all* elements from the framework. This means that we’re serving larger CSS files than we need to. Wouldn’t it be great if there was a way only to include the...

How to minify and add vendor prefixes in Rails 7

If you fire up a new Rails 7 project, you’ll probably be missing a couple of tools you should have: minification and vendor prefixing. Minification takes our JavaScript and CSS assets and squishes everything down by removing whitespace to make file sizes smaller. Adding vendor prefixes helps normalize the way our...

Fixing page jump in Rails websites using Turbo

This was a very annoying problem that had a straightforward fix but was hard to track down.

You have a Rails website using Hotwire/Turbo that is, after clicking a link, skipping to the top before navigating to the requested page. The steps needed to replicate are:

  1. Scroll to the bottom of the...

Regex match for spaces outside of HTML tags

Let’s say you are truncating a blog post to produce an excerpt for multiple posts in an index view. In Ruby on Rails, we can use the truncate() method like:

You might already notice a pitfall with this implementation. By setting escape to false, we are allowing HTML...

How to prepare Rails assets before running tests

I’m currently building a Rails 7 app that was started with rails new myapp --css=bootstrap. This means Rails set up my app to use esbuild to compile all my CSS and JavaScript assets. This new --css flag provided by Rails is a fantastic improvement over previous implementations like...

Using Clearance with Rails 7

For some time now, the Ruby on Rails community has anointed Devise as its preferred authentication solution. I’ve repeatedly used Devise, and it’s great. Not all of us have the time or expertise to roll our own auth, so it’s fantastic a solution exists that is widely adopted. It was only natural that I’d reach fo...

Using private NPM packages with Rails, Heroku and Github

There are numerous reasons why you’d want to leverage the following setup. However, I’m going to be using a trivial example of sharing some CSS styles across different Rails apps via an npm package. That npm package will be private and hosted on GitHub. After we install the package into our Rails project via Yarn...

How to handle time in Rails

Handling time is a doozy. It’s not necessarily a problem that is specific to Rails either. It’s just hard. There are a thousand things to consider. I want to help you get an idea of what a reasonable setup might look like and let you go from there. The following steps are meant to be more conceptual than a line-b...