• Founder, Investor, Advisor

Building a Blog in Rails in 2021

Meet The New Boss, Same As The Old Boss.

December 14, 2021
One of the things I looked forward to as I was getting back to blogging was coding up a blog.  I've done this a couple of times before, and I figured that Rails would be a good tool to try and use, especially since the first popular demo for Rails back in 2005 was a video showing how to build a blog.

Working on the railroad... get it?

If you're reading this post, then you're reading it on the blog I built with Rails 6 and Ruby 3.  Hi!

I have built a bunch of software with Ruby and with Rails but it's been a while since I built a blog.  So, here are my impressions.  The core components of Rails are still as helpful as ever in terms of rapidly creating CRUD (create, read, update, delete) interfaces to the database.  ActiveRecord is a nice ORM and provides the flexibility to drop down to SQL when you need it.  Not too much has changed in terms of that core functionality in all this time.

What has changed?  A few things.  I'm a fan of View Components.  Maybe it's just me, but in the past I had issues with keeping my Rails views from turning into spaghetti code.  There was too much mingling of business logic with markup.  View Components doesn't entirely solve that problem (though I'm sure some of it is just me being lazy) but it sure does help a great deal.  View Components gives you a ruby script coupled with a view template and requires you to declare explicitly all of the variables that will be passed to the script, and through the script to the template.  This makes it much easier to create a clean separation of concerns.  From there it is easy to compose pages using your stable of View Components, and reuse them as needed.

View Components also plays nicely with Stimulus Reflex.  Stimulus Reflex lets you use some simple declarations in your markup that then sets up reactive interfaces without needing React itself.  It provides a way to gradually make your user experience reactive and modern, but exposes a lot less complexity to the developer than something like React would.  For some applications you want access to the complexity and control that React can provide, but for a fairly simple blog, Stimulus Reflex seemed like the right fit to me.

Rails made handling file uploads and rich text editing pretty straightforward with Active Storage and Action Text.  Though I think that with Action Text, Rails may have strayed too far in the direction of convention over configuration.  There were a number of occasions where I wanted to change the behavior of Action Text to align image and text, to have images render appropriately in the email newsletter format of these posts, etc.  Action Text doesn't expose easy ways to configure those things, or much else.  I ended up having to dive into the internals and/or create some helper functions to parse and edit the html that Action Text creates.  Neither of these were a particularly Rails-like developer experience.

I also spent way too much time configuring Action Text's direct upload feature to play nicely with AWS S3 as the storage backend for image attachments.  You would think that this configuration would be well-documented, but it was not in my experience.  For anyone else who might find this and be looking for advice, installing the rack-cors gem was the answer for me.  In retrospect, that makes complete sense, and maybe it should have been obvious, but I suspect there are others who would benefit from having the requirement explicitly called out in the documentation.

After all of that, I still didn't get a rich text editor that I would be particularly happy putting in front of other users, there are too many sharp corners in the UX.  Since I'm the only one blogging here, that doesn't matter much for my use case.  There are certainly other rich text editors you can use out there, but Action Text is what comes bundled with Rails.

I also spent a fair amount of time using Tailwind CSS to style the site.  Rails makes it easy to integrate Tailwind and Tailwind in turn makes it easy to create a responsive interface that looks reasonably good.  Tailwind makes applying CSS more efficient, which isn't saying too much, but every little bit helps.

All in all, I think Rails basically delivers on its mission in 2021, which is to make a developer or a small team of developers productive.  I certainly felt that I was skipping a goodly amount of boilerplate so that I could get on with shipping the blog to production.  The recent additions to the ecosystem of View Components and Stimulus Reflex let me create a reasonably modern UX quickly with little complexity, and that is a significant win in my book.  Though there were a few rough edges, I think if you haven't looked at Rails in a while, it might be time to check back in and see what you've been missing.
Please log in with your Google account to view comments.