Having trouble with turbo stream

I have a stimulus controller that listens for a button click. The button click sends a POST Request (XHR) to a Rails controller do run a background job. When returning from this request, the response is just head :ok.

I would like to use the Rails Flash. So I update the flash.now[:notice] in the controller, and then I try and use turbo. I see in the logs the partial rendering, but then I also get an error as Rails tells me there is no template that matches.

 turbo_stream.replace("my-dom-id", partial: "layouts/flash_messages.html.erb") %>

So I am confused here. I thought I could use turbo to update DOM using replace and that that would work while at the same time, I can respond to the JS request with an OK response.

Apparently not. So, how do you make a simple ajax call and also update DOM with Turbo?

Real quick, one solution could be to wrap the button in a turbo_frame that gets replaced upon success with basically the same button plus a flash message wrapped in a turbo_stream.replace.

See the solution over in this other thread. Does that work?

But what you’re trying to do looks a lot like what Matt Swanson did in this Twitter thread.

I thought of that. Making it so every button press is essentially submitting a special form. I guess I can try that and see how that goes. It seems pretty obvious that one cannot accept a single request from the front-end, and respond with 2 responses ala Turbo + the XHR code. I will try this out, using a form. Thanks.

Honestly though, I’m pretty sure Matt’s example just returns a format.turbo_stream response, which would be simpler.