How to Build a Twitter Clone with Rails 8 and Turbo 8
Hotwire, StimulusJS, and Turbo have been out for the better part of half a decade, and there have been sweeping changes in the Turbo Broadcast and Stream patterns this past year. All of this gets bundled with Rails 8, plus other new features to make development even quicker, such as SolidQueue, SolidCache, and SolidCable. Two of those three make it dead simple to get this clone up and running super quick!
Without further ado, let’s get started with a new app:
Rails will do its thing:
- Install the application.
- Install the gems.
- Process the Bootstrap installation.
- Install the yarn packages.
Hotwire and Turbo are now part of Rails, so installing anything extra is unnecessary! Just fire up bin/dev
and start writing some code.
Now, let’s make a model to hold the data to clone a tweet in this Twitter clone called Blabber. All basic attributes:
To keep this closely resembling the previous article, we’ll add the same validation in the Post
model:
One difference you see is the broadcasts_refreshes
line. This line is part of Turbo 8, where the backend changes (here, changes to the model) are broadcast as a refresh
event in Turbo, to be picked up by the turbo.js
front end. Hidden behind the scenes is all the work Rails does to connect browsers, expecting that particular model instance to have updates and all the connecting parts such as ActiveJob, ActionCable, SolidQueue, and SolidCable.
We’ll make the same few minor adjustments to the generated migration file to add some database-level defaults (and allow us to keep the code around Post
creation simple):
Ok! Now, we’re ready to run that migration!
Another change with this process of refreshes
and streams is that our controller can now be unaware of the streams and does not need to have format-based responses or template files that closely resemble the same partials used for the non-dynamic part of the app.
Simple controller. The index
action returns a list of posts to @post
. create
uses StrongParameters
, creates a new Post, and redirects back to the index
template. like
and repost
are similar, except they increment the respective count columns.
Let’s wire up a few routes to match up to those controller actions. Yes, these aren’t perfect RESTful routes, but 1) They work. 2) This is a 10-minute tutorial. 3) POST requests did not get the other browser window to update with the changes in my small app here. (If anyone knows why, I’d be happy to hear!)
With a Model, Controller, and routes, we can put together some view templates. We can now add the “magic sauce” to the application layout to listen for refresh
broadcasts.
The yield :head
by the rails new
command, and just above it, we added <%= turbo_refreshes_with method: :morph, scroll: :preserve %>
to tell the application to listen for refreshes.
The first view template up is the app/views/posts/index.html.erb
:
The view is a relatively simple Rails template. It uses a render @posts
method to render the collection of @posts
using a posts/_post
partial. The turbo_stream_from:posts
hooks up the view layer to listen to the Post model’s broadcasts.
The partial is some pretty standard singular object markup with some Bootstrap classes thrown in to make it look half-decent:
app/views/posts/_posts.html.erb
Another turbo_stream
command is used to listen for broadcasted updates and deletions, and the dom_id
helper is used to create conventional Rails View IDs model_resource-id
. In this case, it would look like posts_1
, etc.
Up next is a straightforward Rails form:
If you remember the index.html.erb
, another frame wrapped the form’s render call. Thus, we included the dom_id
to allow the controller’s create
action to enable Turbo to manage the streams’ markup.
Now we’ll head back to the controller to talk about the create action in depth:
Previously, at this point, we’d discuss returning streams in the markup to be appended to the top-level frame, but Turbo 8’s refreshes and broadcasts manage all of that without any extra changes.
At this point, you should have an app that you can open in two browsers, which should look like this!
There you go! If everything went well, this was the fastest Twitter clone tutorial yet and probably required the least lines of code! Time will tell how all of this will play well together and scale in complex real-world Rails applications, but for now, the reduction in developer complexity is a huge win!