Ernie Francis

Ruby Railhouse

Ruby Railhouse
Tech Stack:
Chakra UI
Heroku
Postgres
React
Ruby
Ruby Gem
Ruby on Rails

Trying out Ruby on Rails with a react frontend, A top not-so-secret look at my re-introduction with ruby on rails 🤷‍♂️.

By chance or circumstance, This is more an exploration of what ruby on rails can do versus my usual tech stack (node.js and .net). I was always more comfortable with earlier object oriented programming and functional programming via JavaScript and Typescript, either from the IDE (Visual studio, Atom, Visual studio code), enhanced autocompletion and intellisense.

People and organizations today still build product based off ruby on rails, I figured it was a matter of time that I have to re-familiarize myself with the ruby programming language. I already understand the MVC(Model View Controller) concept back when I was coding with ASP.NET core MVC, I just need to translate that knowledge set onto the ruby syntax. Learning the Ruby language itself shouldn't be difficult, I already know a good amount of other languages and common coding practices. It is just a matter of learning the Ruby-specific syntax... ...at least i would learn if I wanted to pollute my laptop with ruby and gem installs. (I know docker is a thing, but ill get to that later...)

Fortunately, I was invited to the GitHub Codespaces beta so I can run a VM with the necessary runtimes enabled at start. thanks to Codespace's first-class integration with Visual Studio code I was able to scaffold my basic ruby on rails app, maintain ALL of my vscode extensions and run a remote dev server accessible via localhost. I can ruby on all the rails and keep my laptop clean 😁.

So far I was able to complete the basic ruby on rails guide, its straightforward MVC with basic CRUD functionality. I do get the appeal of this framework, you can just put this app anywhere with a ruby runtime and it just works! Then again, this framework was released in 2004 before Platform as a Service and Software as a Service became mainstream. The partial views in ruby on rails remind me of MVC architecture trying (keyword is trying) to create reusable components, something React would perfect in 2016 (v15).

A lot of companies that advertise they run with ruby, also have a react frontend! I searched around for ways to implement react with a ruby frontend, I could make a monorepo and just distribute the client bundle file as-is to the ruby on rails workspace, but after some shallow google searches I was able to find react integrations that worked natively with ruby...

Enter React on Rails, a ruby on rails integration to support prerendering react applications. Now i am able to start coding react components against my ruby views. I set off to follow the react setup guide on ShakaCode, integrate react and shakapacker (ShakaCode again) and set up server-side rendering. Unfortunately, the node.js runtime for server-side rendering is behind a paywall, so I'm stuck with execjs for SSR (spoiler alert, I'm not doing that 😝). I did try server-side rendering but exec.js has an ongoing bug posted on GitHub, so ill move forward with client rendering as-is.

enough ranting, I wanted to start coding a react app! I picked the theme for the site thinking about a ruby train riding on the rail tracks all day, eventually the train has to make a stop to pick up or drop off more passengers. I figured a 'railhouse' which sounds like 'alehouse' (i also thought about Ruby Roundhouse from Jumanji, but the theme didn't fit), so I decided to build a webapp for a train-themed alehouse, hence "Ruby's Railhouse".

I re-followed the ruby on rails guide and wanted to whip up a simple CRUD-based webapp. The instructions were pretty straightforward and using my Codespace made scaffolding the dev environment simple, all I had to do on my own was translate the ruby views to react components and inject the ruby context and anti-forgery tokens via props. One thing i liked about this experience was being able to translate ruby's prerendered markup (.erb) and figure our how the ruby form components for the model translated to actual HTML markup, figuring out under the hood how ruby and the web server handle anti-forgery tokens and generate the input fields for each entity. I had the web and UI placeholder set, but i needed a model and a daabase connection.

For the Database, It was considered best practice to switch out the embedded sqlite extension and integrate with a postgres database. Since i was using a code space I couldn't just create a database locally or remote. I used my Heroku account to scaffold the Ruby Railhouse app and add 2 Heroku postgres addons, one for developement/testing and one for production. the database migration was kind of straightforward, Heroku includes a procfile to automate build and release tasks on git push, so i got my CI/CD taken care of.

For the UI, I wanted to use a framework like MUI 5 or Chakra UI. I picked Chakra since most of my projects already use MUI and this project couldn't effectively server-side render thanks to ShakaCode's paywall. I created a single app container and wrapped each React entry point I registered with ruby. I wanted to use Ruby's routing versus client-side routing, see how the code splitting would work per entry point. I used react-hook-forms to make the data display for the CRUD operations. I made a custom context provider to pass on the injected RubyContent and anti-forgery tokens.

For hosting, I just recently connected my GitHub repo to a hobby Heroku app. I used to have to deploy via the Heroku cli manually since GitHub was having integration trouble with Heroku due to a security concern, but I can automate deploys on pushes to the main branch now. I got to say, Heroku is really nice for quickly deploying web servers out of the box. They do use AWS EC2 instances under the hood but they use a predictable cost model versus the egress payment model for AWS.

Overall, Ruby on rails is... okay. its not bad or painfully dreadful, I just believe that there are other tech stacks that make the conventional MVC model obsolete. I could just be speaking from bias since I'm more object-oriented, I may have to explore this repo more and figure out new features for the railhouse app. I would also nee to get more familiar with the ruby syntax, my IDE (vscode) doesn't have world-class ruby support and I don't feel like paying for RubyMine just yet.

Other developers may say the same thing about my preferred tech stacks (ASP.NET core, Next.js, blitz.js, Node.JS) citing ruby on rails is the GOAT (Greatest of all Time). I can admit I wrote this article with "growing pains" while coding with ruby, I can certainly read the syntax just fine.