Turbo stream is not updating my view on rails 7.0 application

Viewed 1539

I have a new rails 7.0 application. I am trying to load a new turbo stream with no luck. Here is my code. On my index.html page I have the following

<form action="/welcome/remove_stream" method="post" >
  <label for="query">Search for:</label>
  <input id="query" name="query" type="text" />
  <input name="commit" type="submit" value="Remove" data-disable-with="Search" />
</form>

 <div id="messages">The messages</div>

Then in the controller I have an empty method

def remove_stream
end

And on the corresponding view. remove_stream.html.erb I have the following.

<turbo-stream action="append" target="messages">
  <template>
    <div id="message_1">
      This div will be appended to the element with the DOM ID "messages".
    </div>
   </template>
</turbo-stream>

When I submit the form I get the 200 OK but the element is not been appended.

2 Answers
  • you might to add respond_to to your controller method
  def remove_stream
    respond_to do |format|
      format.html { redirect_to quotes_path, notice: "Quote was successfully destroyed." }
      format.turbo_stream
    end
  end
  • make sure turbo_stream_from in your index.html
<%= turbo_stream_from "streams" %>

How about creating remove_stream.turbo_stream.erb instead of remove_stream.html.erb.

remove_stream.turbo_stream.erb

<%= turbo_stream.append("messages", "<div id='message_1'>
  This div will be appended to the element with the DOM ID 'messages'.
</div>") %>

You can even create a _message.html.erb partial for cleaner code.

Related