Hotwire Discussion

Using form helper in Turbo Stream broadcast

I’m having trouble getting something like the following to work:

# app/models/todo.rb
class Todo < ApplicationRecord
  after_create_commit -> { broadcast_append_to :todos }
end
# app/views/todos/_todo.html.erb
<%= form_with model: todo do |f| %>
  <%= f.check_box :done, onchange: 'this.form.requestSubmit();' %>
  <%= todo.text %>
<% end %>

No session is available during HTML rendering in the broadcast, so in attempting to generate an authenticity token, it raises: ActionView::Template::Error (Request forgery protection requires a working session store but your application has sessions disabled. You need to either disable request forgery protection, or configure a working session store.)

Without the form tag, it works as expected (very closely following the tutorial video at https://hotwired.dev/). Or, without the broadcast and instead doing the stream append in the template response, it works as expected, but obviously will only stream to the current client.

I’ve seen in a few places that something along these lines may not be possible. Is that true? It seems like a fairly common use case to capitalize on template reuse. I’ve also seen some hacky workarounds but am wondering if I’m just missing out on the “official” way of doing what I’m trying to achieve here.