Search code examples
ruby-on-railsformsturbolinksturbohotwire

How can I disable Hotwire / Turbo (the Turbolinks replacement) for all forms in a Rails application?


In Rails 7, Turbolinks is replaced by Hotwire / Turbo. This patches up web links so that they become AJAX-style requests just like Turbolinks did, but also patches up forms. On the whole, we found this broke swathes of our application.

  • Forms that already had some kind of JS behaviour attached conflicted.
  • Standard Rails error handling idioms - "set the flash and render :new/render :edit" - break, astonishingly; one must add status: :unprocessable_entity and cross fingers.
  • If the controller handling a form submission redirects to a URL that includes a patch anchor (...#foo), Turbo strips it.

In the end, there's only so much data: {turbo: false} it makes sense to scatter all over your code base. Worse, we're using SimpleForm, so this becomes the even-more-cumbersome html: {data: {turbo: false}}.

Is there a way to _globally disable Turbo just for forms (ideally all forms, whatever their origin - just leave the <form> tag and everything inside it completely alone please) but leave the rest of its behaviour unchanged?


Solution

  • Wish granted, although, undocumented. Available since turbo v7.2.0 (turbo-rails v1.3.0).

    // app/javascript/application.js
    
    Turbo.setFormMode("on" | "off" | "optin")
    
    // for turbo v8.0.6
    Turbo.config.forms.mode = "on" | "off" | "optin"
    

    "on" - default - Turbo forms all the time. Use data-turbo="false" to disable turbo on individual forms.

    "off" - No turbo forms ever. data-turbo="true" is ignored.

    "optin" - No turbo forms unless you insist. Use data-turbo="true" to enable turbo on individual forms.


    https://github.com/hotwired/turbo/pull/419