Auto-saving Rails forms with Turbo Streams
by Kerrick on 1/9/2025, 4:45:54 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(); } }
<%= 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 %>
by xutopia on 1/9/2025, 4:51:49 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.
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].
and [0]: https://stimulus.hotwired.dev/reference/controllers#properti...