Skip to content

Comment on Auto-saving Rails forms with Turbo Streams

Comments

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...
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.