Auto-saving Rails forms with Turbo Streams

by strzibnyon 1/9/25, 1:29 PMwith 3 comments
by Kerrickon 1/9/25, 4:45 PM

Forgive me if I'm wrong, but I think you can avoid the extra `<div>` element and `formTarget`. You can use `this.element` to do so [0].

    import { Controller } from "@hotwired/stimulus";
    
    export default class extends Controller {
      submit(event) {
        event.preventDefault();
        this.element.requestSubmit();
      }
    }
and

    <%= form_with(model: post, url: post_path(post), data: { "controller": "autosave" }) do |form| %>
      <%= form.label :title %>
      <%= form.text_field :title, class: "input", data: { action: "blur->autosave#submit" } %>
        
      <turbo-frame id="title-status">
      </turbo-frame>
    <% end %>
[0]: https://stimulus.hotwired.dev/reference/controllers#properti...

by xutopiaon 1/9/25, 4:51 PM

Honestly Turbo Streams and Hotwire have been a game changer for me. I can do so much with so little code. I can’t believe so many people are stuck in react hell.