Disabling Turbo for form submissions in your Rails app
How to disable turbo for form submissions in your Rails app with the data-turbo="false" attribute.
In a previous post you can read about here, we addressed the fact that form submissions in rails apps created using Rails 7 and beyond are processed with the Turbo Stream format instead of HTML.
While this is great for making your app faster through seamless multi-page navigation, there may arise an instance where you may need these form submissions to be processed as HTML.
Disabling turbo for a form in a rails app is as simple as using the data-turbo=”false” attribute. We structure the form as below, specifying as part of the form_with helper tag that turbo should be disabled for this particular form.
_form.html.erb:
And when we check our server logs, for example after creating a new post, we’ll see that the “post” request which is associated with creating a new post is processed as HTML instead of TURBO STREAM.
Resorting back to the default would be as simple as removing the data-turbo=”false” attribute from the form_with helper tag and the form submissions will be processed as TURBO STREAM which is the default format for form submissions as of Rails 7.
In a previous post, i posed a question for my readers about whether requests associated with the redirect_to method will be processed as HTML or TURBO STREAM and the answer to this question is that, “requests associated with redirecting will be processed as TURBO STREAM since the major reason for using Turbo is to do away with full-page reloads and redirects”.
And that marks the end of this post. If you’d love to receive posts similar to these direct to your inbox, consider subscribing to the publication by clicking the button below.