HOTWiring an existing Rails Monolith: Forms!

Let’s say your majestic monolith is looking for more interactivity, and you’ve picked up Turbo. What aspects of a Rails app change that may cause some headaches? Add Turbo, and then follow along!

Forms

First, you need to test all your forms. Turbo dramatically changes the behavior of each form. The biggest change you will see is that your form redirects don’t behave the same way. Since Turbo is adding Single Page Application (SPA) functionality to your app, you will likely need to reengineer the frontend to behave more like a SPA. I think the quickest way to get Turbo working, and give you flexibility to incrementally update your forms is turn off remote forms, and tell Turbo to ignore the form. This worked best on my forms that made some change, and then redirected to a new page. This meant adding some data-turbo and data-remote attributes, like so:

<%= form_with model: @order, url: orders_path, data: { turbo: false, remote: false } do |form| %>

Setting the remote and turbo data attributes to false makes the form submission occur outside the JavaScript environment. This will blow away the JavaScript environment, so you’ll want to figure out a way reengineer your forms, which I’ll show you in the next section.

I don’t want to indict Turbo at all for this change. Adding Turbo is a rethinking of the way your Rails app interacts with the browser, and this is a stop gap to your existing app works, and you can refactor the front end to include more interactivity.

Leave a Reply