class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
respond_to do |format|
if @comment.save
format.turbo_stream
format.html { redirect_to @post, notice: "Comment added." }
else
format.turbo_stream do
render turbo_stream: turbo_stream.replace(
"new_comment",
partial: "comments/form",
locals: { post: @post, comment: @comment }
), status: :unprocessable_entity
end
format.html { render "posts/show", status: :unprocessable_entity }
end
end
end
private
def set_post
@post = Post.find(params[:post_id])
end
def comment_params
params.require(:comment).permit(:body, :author)
end
end
<%= turbo_stream.append "comments" do %>
<%= render partial: "comments/comment", locals: { comment: @comment } %>
<% end %>
<%= turbo_stream.replace "new_comment" do %>
<%= render partial: "comments/form", locals: { post: @post, comment: @post.comments.build } %>
<% end %>
<%= turbo_stream.update "comments_count" do %>
<%= pluralize(@post.comments.count, "comment") %>
<% end %>
<section id="comments_count">
<%= pluralize(@post.comments.count, "comment") %>
</section>
<%= turbo_frame_tag "comments" do %>
<div id="comments">
<%= render partial: "comments/comment", collection: @post.comments %>
</div>
<% end %>
<div id="new_comment">
<%= render partial: "comments/form",
locals: { post: @post, comment: @post.comments.build } %>
</div>
<%= form_with model: [post, comment], id: "new_comment_form" do |f| %>
<% if comment.errors.any? %>
<ul class="errors">
<% comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= f.text_field :author, placeholder: "Your name" %>
<%= f.text_area :body, placeholder: "Write a comment\u2026" %>
<%= f.submit "Post comment" %>
<% end %>
This snippet shows how a Rails controller can serve a single create action to two very different clients: modern browsers running Turbo, and older or scripting clients (crawlers, integration tests, users with Turbo disabled) that only understand plain HTML redirects. The technique is content negotiation via respond_to, where the response format drives whether the server streams a DOM patch or falls back to a full-page redirect.
In CommentsController, the key is that Turbo registers a custom MIME type, text/vnd.turbo-stream.html, and advertises it in the Accept header on form submissions. The respond_to block branches on format.turbo_stream versus format.html. When the request accepts Turbo Streams, render turbo_stream: sends only the small fragment needed to append the new comment and reset the form. When it does not — the fallback path — the controller issues a classic redirect_to, so the page simply reloads and the comment appears through normal server rendering. The else branch on validation failure mirrors the same split: a turbo_stream.replace for the errored form fragment, or a re-rendered :new with unprocessable_entity for HTML.
Because both paths must produce the same markup, the view logic lives in shared partials rather than being duplicated. create.turbo_stream.erb composes turbo_stream tags that reference comments/comment and comments/form, the exact partials the full HTML page also renders. This is what makes the fallback truly graceful: there is one source of truth for how a comment looks, and Turbo just delivers a surgical slice of it.
The index.html.erb tab ties it together with a turbo_frame_tag and a DOM id (dom_id) that the stream targets. The trade-off worth noting is discipline around ids: the append target and the wrapping element must agree, or the stream silently does nothing. This pattern is worth reaching for whenever an app should feel like an SPA for capable clients while remaining fully functional — and testable, and SEO-friendly — for everyone else, without maintaining two rendering codebases.
Related snips
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
System test: asserting Turbo Stream responses
class Post < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :comments, dependent: :destroy
scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.current) }
scope :draft, -> { where(published_at: nil) }
ActiveRecord scopes for reusable query logic
module Api
module V1
class UsersController < BaseController
def show
user = User.includes(:profile).find(params[:id])
ETags for conditional requests and caching
class PostsController < ApplicationController
def index
@posts = Post.includes(:author)
.order(created_at: :desc)
.page(params[:page])
.per(10)
Turbo Frames: infinite scroll with lazy-loading frame
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
<form data-controller="query-sync" data-action="change->query-sync#apply">
<select name="status" class="rounded border p-2">
<option value="">Any</option>
<option value="open">Open</option>
<option value="closed">Closed</option>
</select>
Filter UI that syncs query params via Stimulus (no front-end router)
Share this code
Here's the card — post it anywhere.