RSSAmplifier

Blog

Andy Croll

andycroll.comRSS feed ↗15 posts

Latest posts

Use class_names to Conditionally Apply CSS Classes

When you’re building views in Rails, you often need to apply CSS classes conditionally. Maybe a nav link should look different when it’s the current page, or a form field needs error styling. Since Rails 6.1, the class_names helper does this cleanly. Instead of… …interpolating conditional classes with ternaries or post-statement conditionals: <div class= "p-4 rounded <%= @error ? 'bg-red-50…

Avoid html_safe with Tag Helpers, safe_join, and sanitize

When you need to build HTML outside of a template, it’s tempting to concatenate strings and call html_safe on the result. This bypasses Rails’s built-in XSS protection entirely: any user input in that string goes straight to the browser unescaped. The good news is you almost never need html_safe . Rails provides three underappreciated tools that handle escaping for you. Instead of… …calling…

Use Rails Combined Credentials

To deal with secrets and credential handling most Rails apps have ended up with a hotchpotch of ENV.fetch calls and credentials.dig lookups throughout the codebase, depending on where each secret lives. Rails edge — and the upcoming 8.2 — fixes this. Instead of… …mixing ENV and credential lookups: class StripeChargeService def initialize @api_key = ENV . fetch ( "STRIPE_API_KEY" ) @webhook_secret…

Teach Rails Irregular Plurals with Inflections

English has plenty of irregular plurals. Criterion becomes criteria, not criterions. Rails handles many common ones already, but your domain might include words it doesn’t know about. Instead of… …accepting Rails’s best guess at a plural: "criterion" . pluralize #=> "criterions" "matrix" . pluralize #=> "matrices" # this one Rails knows! Use… … inflect.irregular to teach Rails the correct pair: #…

Handle Uncountable Words in Rails Inflections

Some English words don’t have a separate plural form. “Staff” is staff, “metadata” is metadata, “feedback” is feedback. Rails doesn’t always know this—it will happily generate a staffs table or a metadatas route if you let it. Instead of… …fighting Rails when it pluralises words that shouldn’t change: "staff" . pluralize #=> "staffs" "metadata" . pluralize #=> "metadatas" "feedback" . pluralize…

Declare Acronyms in Rails Inflections

A lot of Rails’s naming magic comes from its clever use of inflections. user.rb defines the User class, backed by the users table, managed by UsersController , accessible at the /users/ routes. Every Rails app generates config/initializers/inflections.rb to let you customise this behaviour. Most developers leave it empty. Then one day you namespace a controller under API and Rails starts…

Group Repeated Options with with_options

When multiple validations share the same if: condition, or multiple callbacks share the same only: constraint, you end up repeating yourself. with_options groups them together. Instead of… …repeating conditions across multiple validations: class Article < ApplicationRecord validates :title , presence: true , if: :published? validates :body , length: { minimum: 100 }, if: :published? validates…

Customize Model URLs with to_param

Rails models default to using their ID in URLs: /articles/42 . The to_param method lets you customize this—use a slug, hide the ID, or combine both for readable URLs. Exposing IDs isn’t dangerous if you scope access properly, but you might want cleaner paths. Instead of… …exposing IDs in your URLs: redirect_to article_path ( @article ) # => "/articles/42" Use… …the to_param method to return a slug…

Use StringInquirer for Readable Predicate Methods

You’ve probably seen Rails.env.production? in your codebase to ensure that certain code only runs in production. Instead of having to compare strings, Rails.env == "production" , Rails wraps the string in an ActiveSupport::StringInquirer so you get readable methods like .production? and .development? . Active Support also adds an inquiry method to String so you can use this same pattern in your…

Prefer in? Over include? for Readable Conditions

When checking if a value exists within a collection, Ruby’s include? method does the job, but Rails provides a more natural alternative through Active Support’s in? method . Instead of… …reading your conditions backwards with include? : nsync = [ "Justin" , "JC" , "Chris" , "Joey" , "Lance" ] if nsync . include? ( candidate ) puts " #{ candidate } is in the band" end # Or inline if [ "Justin" ,…

Simple Tailwind CSS 4 Setup for Jekyll

Tailwind CSS 4 changed how configuration works. The JavaScript config file has been replaced by CSS-based configuration using @theme directives and uses the tailwind CLI to shake down the generated tailwind classes and minify. Here’s how to set it up with Jekyll . The Setup Changes to four files, plus one more step if you want plugins. Gemfile gem "jekyll-tailwind" , group: [ :jekyll_plugins ] Run…

Find the Last Matching Element with rfind

Ruby 4.0 landed during Christmas 2025 with a bunch of new features. One small but useful addition is Array#rfind , which finds the last element in an array that matches a condition. Instead of… …reversing the array or using reverse_each to find from the end: numbers = [ 2 , 2 , 3 , 4 , 6 , 7 , 8 ] numbers . reverse . find ( & :odd? ) #=> 7 # or numbers . reverse_each . find ( & :even? ) #=> 8 Use……

Year in Review 2025

House moves, robot armies, and scratching itches. January—February Ten years ago, I was looking at the prices of T-shirt printing machines and figuring out the best way to manage stock and postal services. I’ve always wanted to have a place to exercise my design eye and this year I launched Made the Scene . I use a mix of Shopify and Printful and add designs whenever they come to me. I don’t have…

Skip Validations in Specific Contexts with except_on

Rails 8.0 added except_on as an option to validations . It’s the inverse of on: and lets you skip validations in specific contexts. Instead of… …skipping all validations in your controller: class Admin::UsersController < ApplicationController def create @user = User . new ( user_params ) if @user . save ( validate: false ) # Skips ALL validations! redirect_to admin_users_path else render :new end…

Rails World 2025

Off I went to Amsterdam via—so incredibly civilized—Eurostar from St Pancras wearing my favourite Made the Scene t-shirt. Had a day mostly to myself in Amsterdam on Wednesday, drinking coffee, eating chocolates , meeting friends, visiting the Anne Frank House and generally enjoying the vibe of the city while avoiding its insalubrious corners. I even mananged a run around the city before the…