The RailsNotes Newsletter 🟥 ISSUE #8

🟥 ISSUE #8 (better Stimulus controllers, the perfect 'jumelage', practical Stimulus)

Me finally getting my Stimulus controller to work 🧙‍♂️

— SPONSOR —

RailsNotesUI ActionMailer Component library

I’m launching my ActionMailer components library!

Add emails to your Rails apps fast with pre-built mailer components & templates 📬💨

Use the code LAUNCH20 for a special, newsletter-only discount 😎

Welcome to The RailsNotes Newsletter — Issue #8!

This week we hit 445 subscribers! 🥳

It’s always exciting to see more subscribers joining RailsNotes! My next major milestone is 1000 subscribers. If you know anyone who would enjoy this newsletter, please forward it along! It helps me out heaps 😇 

I also have an exciting announcement — I’m officially launching my ActionMailer component library! You can browse the components and templates without buying anything, so check them out! And if you decide to purchase a license (which also helps support this newsletter), make sure to use the newsletter-exclusive discount code LAUNCH20 to get 20% off!

I’ve had great feedback from the beta group (I sent out an email today to the beta cohort with instructions on activating your beta accounts!) and it’s been great to see what everyone’s been building!

I spent most of this week re-building railsnotesui.xyz to prepare for the launch 🙉. Personally, I think it turned out pretty great!

The new and improved RailsNotes UI site, ready for the ActionMailer email component release.

I may have taken some inspiration from Tailwind UI…. 😅. Building the site gave me great chances to investigate unfamiliar parts of Rails (ActionView template handlers, anyone?), and there’s a lot of great stuff I’m planning to pull out into new articles.

Today’s featured article is the first of those extracted articles! When I was building the component section, I needed a Stimulus controller to toggle between different “tabs”. I’ve extracted that controller into its own article, and I’ve decided to theme this week’s newsletter around Stimulus controllers.

I’ve also included some great articles further down from BoringRails, Thoughtbot, and Akshay Khot’s blog, which all have great tips for building clean, composable Stimulus controllers.

Let’s dive in!

FEATURED ARTICLE —

I’d basically given up on MidJourney when I asked it for a “ruby on rails stimulus tabs controller”… and somehow got this. Look…. it’s the best I could do 😅 Go AI… I guess? 🦾 

Stimulus is one of the core parts of the “Hotwire” stack, the Rails 7 way to build responsive frontends. Time and time again, I’m blown away by what we can build with 99% HTML, and a few sprinkles of Stimulus-powered Javascript.

This article is a pretty short one — it’s mostly just code for the Stimulus controller, but I still think it’s valuable as a reference. It’s part of a longer-term goal of mine to build a collection of Stimulus controllers on the blog. Then, you’ll be able to grab the ones you need for your Rails apps, without having to hunt around for them on different websites and GitHub repos.

One important thing for me is sharing the code for Stimulus controllers directly, rather than packaging them into a gem. This way, you can grab the raw code, understand it, and edit it to suit your app.

Hope you enjoy this article! And keep an eye out for more coming your way 😉

🌐 MORE ARTICLES —

Another week, another BoringRails article 🤭. Trust me though, this article is top-tier. It goes deep into writing clean, composable Stimulus controllers, something that, for many, will be a new way of thinking.

Controller bloat seems like a real problem for a lot of Rails devs — if you’re not careful, you can end up with heaps of super-specific controllers for niche behaviours (toggle-hidden, toggle-active, toggle-enabled etc.). Matt Swanson flips that on its head and shows you how to write generic controllers that you use across your entire app.

This article is the perfect “jumelage”(french: ‘pairing’) to the one above. Instead of going deep (like the BoringRails article), this one goes wide and walks you through different patterns to help write better Stimulus controllers.

It contains incredible amounts of tactic, hard-won knowledge. It’s got great examples for the Classes API, Events, writing composable controllers, and more.

I’ve always found the best way to learn something new, is by diving in and tinkering with it. If you’re new to Stimulus, this is a great place to dive in! 🤿 Follow along with Akshay as he builds a simple text widget, and covers everything you need to start using Stimulus in your own Rails apps along the way.

This is a relatively short article and a great Stimulus starting point. If you’re hungry for more once you’re done, I wrote my own introduction to Stimulus article here — Learn Stimulus building a toggle.

— ⚒️ HANDY TIP — 

→ Quickly generate a new Stimulus controller with rails g stimulus

Rails includes tons of handy generators, so it shouldn’t come as a surprise that there’s one for Stimulus controllers too (you can read the code for it here, on GitHub).

This generator creates the controller for you, then runs stimulus:manifest:update if needed.

Bonus tip: you can see all the Rails generators by running rails g. You can then search all the generators with rails g | grep stimulus (replace “stimulus” with something like “rspec” or “model”).

I use this all the time, it’s super (duper) handy!

# quickly generate a new stimulus controller
# auto-runs stimulus:manifest:update (if needed)
#

> rails g stimulus tab
    create app/javascript/controllers/tab_controller.js