I was experimenting with Turbo Morph and was surprised by the number of things we can achieve with plain Ruby on Rails and a bit of Turbo Morph/Stream.
So, I decided to put together a quick example of creating a highly reactive and responsive UI without JavaScript.
I’ll be using Rails 8 and Tailwind CSS. Here are the steps to set up everything we need:
$ rails new app --css=tailwind
$ rails g scaffold Item title:string
$ bin/rails db:migrate
Great, we have our views and models set up.
Building the Interface
First, let’s work on our interface to mimic the mockup. [Commit]
Next, let’s add a title validation to avoid creating empty items. [Commit]
Now, I’ll update the controllers to redirect to the list index action after creating an item. [Commit]
Reviewing Our Progress
Let’s have a quick look at what we have so far. Every time I delete or remove an item, Turbo comes into action and replaces the body of the page with the new HTML. Pretty cool, as it avoids a full page reload. However, there are a few things that aren’t ideal:
1. The scroll position is not preserved.
2. The entire body is replaced.
default-turbo-sm.mp41.62 MB
Enhancing with Turbo Morph
Let’s use Turbo Morph by adding the following line to the layout to address these two issues: [Commit]