Skip to main content

Category: Ruby on Rails

A Simple Solution for Combining Different Data Types in Rails

When building Humadroid’s dashboard, I needed to show both company announcements and employee shoutouts in a single feed. At first, I took what seemed like the easy route - using notifications as a bridge between these different types of content. Each announcement or shoutout would create a notification, and I’d just query those. Simple enough, right?

Well, not quite. I soon realized this approach had a major flaw: new users couldn’t see older announcements or shoutouts. They’d join the company and find an empty dashboard. Not exactly the welcoming experience I was aiming for!

Quick Tip: Reading Ruby Version from .tool-versions in Your Gemfile

While migrating from asdf to mise as my version manager, I needed to update how my Gemfiles read the Ruby version. It’s a small change, but I thought it might be useful to share.

With asdf, I was using .ruby-version:

ruby File.read(File.expand_path('.ruby-version', __dir__)).chomp

Now with mise using .tool-versions, which can contain versions for multiple languages, the line changed to:

ruby File.read(File.expand_path(".tool-versions"))[/^ruby[@ ](.+)$/, 1]

The regex matches a line starting with “ruby” (followed by either a space or @ symbol) and captures the version number.

A Simple Fix for Dynamicly Added External Widgets in Turbo-Enabled Sites

While working on Humadroid, I recently stumbled upon an interesting challenge with our chat widget. It’s a small discovery, but one that might save other developers some headaches.

The Problem

If you’re using Turbo (formerly Turbolinks) and external widgets like Chatwoot, you might notice they disappear or reload whenever you navigate between pages. This happens because these widgets usually attach themselves to the end of your DOM, and Turbo replaces all that content during page transitions.

Nested forms with just Turbo Streams

Edit 11.12.2024: I’ve updated turbo stream responses to include field index.

Recently I was working on implementing dynamic nested forms using Turbo Streams, focusing on handling both new and persisted records without custom JavaScript. Well, mostly for fun, to try something new. And while there are multiple approaches to this problem, including Stimulus controllers or plain JavaScript, Turbo Streams offer a clean, server-driven solution that leverages Rails conventions.

The Core Concept: Form Manipulation vs Persistence

A key aspect of this implementation that might not be immediately obvious is that the IngredientsController doesn’t actually persist any data. Its sole responsibility is to manipulate the form structure through Turbo Stream responses.