Writing better Action Mailers: Revisiting a core Rails concept
Boring Rails
by Matt Swanson
1y ago
Mailers are a feature used in literally every Rails application. But they are often an after thought where we throw out the rules of well-written applications. Writing mailers is a “set it and forget it” part of your codebase. But recently, I’ve revisited the handful of mailers in my application and I was shocked at both how bad things were and also how many nice mailer features in Rails I wasn’t aware of. I’ve been writing Rails applications for over 10 years and there were things I figured out just this week about mailers that I will be using as my new defaults going forward. Psst! If you li ..read more
Visit website
Sorting ActiveRecord results by enum values (in SQL)
Boring Rails
by Matt Swanson
1y ago
Rails enums are a great way to model things like a status on an ActiveRecord model. They provide a set of human-readable methods while storing the result as an integer in the database. class JobSubmission < ApplicationRecord enum status: { draft: 0, submitted: 1, hold: 2, rejected: 3, accepted: 4, canceled: 5 } end It is highly recommended to use a Hash to explicitly define the enum values – otherwise Rails will use the index of the enum value when storing it to the database. If you were were to change the order or remove options, you would break the reference ..read more
Visit website
Thinking in Hotwire: Progressive Enhancement
Boring Rails
by Matt Swanson
1y ago
This post is part of Hotwire Summer: a new season of content on Boring Rails! There are many tutorials about how to get started with Hotwire and how to use the individual pieces. But one thing that took me a while to grasp was how to “think in Hotwire”. Hotwire itself is an overarching concept (HTML-over-the-wire) and you’ll need to know when to use the different pieces (Turbo Drive, Frames, Streams, Stimulus, Turbo Native, Strada). Because Hotwire is a collection of tools, you can solve problems multiple ways. There are features you can build with Frames that you could also build with Streams ..read more
Visit website
Galaxy brain CSS tricks with Hotwire and Rails
Boring Rails
by Matt Swanson
1y ago
This post is part of Hotwire Summer: a new season of content on Boring Rails! In Hotwire applications, you need to lean more on the fundamentals of CSS and HTML. If you’re like me, you probably learned just enough CSS to get by, but never reach for it first. But that’s changed recently and I wanted to share patterns I’ve picked up recently that improve my Rails apps. Empty States and Turbo Streams An extremely common pattern in Rails apps is rendering a collection of elements and if the collection is empty, render an empty state. <div id="my_list" class="flex flex-col divide-y"> < ..read more
Visit website
Adding keyboard shortcuts and hotkeys to StimulusJS
Boring Rails
by Matt Swanson
1y ago
This post is part of Hotwire Summer: a new season of content on Boring Rails! Keyboard shortcuts are a great way to level up your user experience and improve the accessibility of your web applications. Even if you aren’t ready for advanced hotkey schemes, small things like binding the Escape key to close a modal can have a big impact. Stimulus doesn’t come with built-in support for hotkeys. As part of a recent project at Arrows, I evaluated the ecosystem and wanted to share my thoughts. stimulus-hotkeys This package provides a hotkeys controller and uses a JSON object to map keyboard shortcuts ..read more
Visit website
The most underrated Rails helper: dom_id
Boring Rails
by Matt Swanson
1y ago
This post is part of Hotwire Summer: a new season of content on Boring Rails! The dom_id helper in Rails is over a decade old, but has proven to be an invaluable concept in Hotwire. This secret workhorse powers all kinds of HTML-related behavior in Rails. It has one key job: making it easy to associate application data with DOM elements. dom_id takes two arguments: a record and an optional prefix. The record can be anything that responds to to_key and model_name, but 99% of the time you are passing it an ActiveRecord model. The prefix can be anything that responds to to_s, but 99% of the time ..read more
Visit website
Self-destructing StimulusJS controllers
Boring Rails
by Matt Swanson
2y ago
This post is part of Hotwire Summer: a new season of content on Boring Rails! Sometimes you need a little sprinkle of JavaScript to make a tiny UX improvement. In the olden days, full-stack developers would often drop small jQuery snippets straight into the page: <script type="application/javascript"> $(".flash-container").delay(5000).fadeOut() $(".items").last().highlight() </script> It got the job done, but it wasn’t the best. In Hotwire apps you can use a “self-destructing” Stimulus controller to achieve the same result. Self-destructing? Self-destructing Stimulus control ..read more
Visit website
Tailwind style CSS transitions with StimulusJS
Boring Rails
by Matt Swanson
2y ago
This post is part of Hotwire Summer: a new season of content on Boring Rails! If you’ve built UI elements with StimulusJS before, you’ve certainly written code to show or hide an element on the page. Whether you are popping up a modal or sliding out a panel, we’ve all written controller code like: this.modalTarget.classList.remove("hidden") To take your UI designs to the next level, you can use transitions so elements don’t immediately appear or disappear from the screen. You can transition opacity to gently fade elements in and use translate to slide them into place. One issue trying to do ..read more
Visit website
Dynamic user content in Rails with Liquid tags
Boring Rails
by Matt Swanson
2y ago
When building features that accept user-generated content, you may need to display dynamic content based on what the user specifies. Imagine you want to users to be able to customize a welcome message sent from your application when they invite someone to their account. Rails programmers are deeply familiar with writing content with pieces of dynamic text: we do this all the time when writing view templates. But we don’t want to allow users to write ERB or HAML strings and execute them in our app. It’s both a huge security risk and also not super friendly for users to have to learn a complete ..read more
Visit website
Accessing Rails environment variables from a StimulusJS Controller
Boring Rails
by Matt Swanson
2y ago
Environment variables are a great way to configure your Rails apps. You can use the Rails.env variable to conditionally change behavior when you are in development/test or production. And you can add your own application specific variables for things like API tokens or global settings. While Stimulus has a values API to read from HTML data attributes, sometimes you need data that you don’t want to pass in every time you create a controller. One alternative is to use <meta> tags when rendering your view. Then, inside your Stimulus Javascript code, you can query the <meta> DOM elemen ..read more
Visit website

Follow Boring Rails on FeedSpot

Continue with Google
Continue with Apple
OR