I have built a very simple test application (https://github.com/Chrisgo-75/rls7_docker_test_app/tree/main) that demonstrates the issue I am seeing on my production app. I have ruined my production app in trying to unsuccessfully get turbo to work.
The issue is that Rails 7 application is not parsing the turbo_stream request but instead downloads the file app/views/posts/create.turbo_stream.erb
. When I try to create a new post is when the app downloads the turbo_stream file. With Rails 7 I thought Turbo should work out-of-the-box?
Any suggestions on how to get turbo_streams to work?
Thank you
Chris
It does work fine. Make sure you understand that turbo_streams
are for updating your current page and you're not using the turbo_stream.append
helper correctly:
<!-- app/views/posts/index.html.erb -->
<div id="post_form">
<%= render "form", post: Post.new %>
</div>
<!-- `id="posts"` will be the target of the turbo stream -->
<div id="posts">
<% @posts.each do |post| %>
<%= render post %>
<p>
<%= link_to "Show this post", post %>
</p>
<% end %>
</div>
Do not add turbo_stream
url extension:
# app/views/posts/_form.html.erb
<%# form_with(model: post, format: :turbo_stream) do |form| %>
<%= form_with model: post do |form| %>
...
<% end %>
# app/controllers/posts_controller.rb
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.turbo_stream do
render turbo_stream: [
# `id` target vvvvv
turbo_stream.append(:posts, @post),
# reset form
turbo_stream.update(:post_form, partial: "form", locals: {post: Post.new}),
]
end
format.html { redirect_to post_url(@post), notice: "Post was successfully created." }
else
format.turbo_stream do
# render errors
render turbo_stream: turbo_stream.update(:post_form, partial: "form", locals: {post: @post})
end
format.html { render :new, status: :unprocessable_entity }
end
end
end