<%= turbo_frame_tag dom_id(flag) do %>
<tr class="flag-row">
<td class="flag-name"><%= flag.name %></td>
<td class="flag-env"><%= flag.environment %></td>
<td class="flag-state">
<span class="badge badge--<%= flag.enabled? ? 'on' : 'off' %>">
<%= flag.enabled? ? "Enabled" : "Disabled" %>
</span>
</td>
<td class="flag-action">
<%= button_to toggle_admin_feature_flag_path(flag),
method: :patch,
class: "toggle toggle--#{flag.enabled? ? 'on' : 'off'}",
data: { turbo_stream: true } do %>
<%= flag.enabled? ? "Turn off" : "Turn on" %>
<% end %>
</td>
</tr>
<% end %>
module Admin
class FeatureFlagsController < Admin::BaseController
before_action :set_flag, only: :toggle
def index
@flags = FeatureFlag.order(:name)
end
def toggle
@flag.update!(enabled: !@flag.enabled?)
respond_to do |format|
format.turbo_stream
format.html { redirect_to admin_feature_flags_path, notice: "Updated #{@flag.name}" }
end
end
private
def set_flag
@flag = FeatureFlag.find(params[:id])
end
end
end
<%= turbo_stream.replace dom_id(@flag) do %>
<%= render "feature_flag", flag: @flag %>
<% end %>
<%= turbo_stream.update "flash" do %>
<div class="flash flash--notice">
<%= @flag.name %> is now <%= @flag.enabled? ? "enabled" : "disabled" %>.
</div>
<% end %>
Rails.application.routes.draw do
namespace :admin do
resources :feature_flags, only: :index do
member do
patch :toggle
end
end
end
end
This snippet shows how a Rails admin panel implements an inline "quick toggle" — flipping a boolean like published or active without a full page reload — using Turbo Streams and a single shared partial. The core idea is that the toggle control and its server response render from the same partial, so there is exactly one source of truth for how the row looks in either state.
In _feature_flag.html.erb, the partial wraps everything in a turbo_frame_tag keyed by the record (dom_id). Inside it, button_to posts to a member toggle route with method: :patch and data: { turbo_stream: true }, which tells Turbo to expect a text/vnd.turbo-stream.html response rather than a redirect. The button label and CSS class are derived from flag.enabled?, so the exact same markup describes both the on and off appearance.
In FeatureFlagsController, the toggle action loads the record, flips the column with update!, and then respond_to. The format.turbo_stream branch renders toggle.turbo_stream.erb; the format.html branch redirects, which keeps the feature usable when JavaScript is unavailable or when the request comes from a non-Turbo client. This progressive-enhancement fallback is a deliberate trade-off — a little duplication for graceful degradation.
In toggle.turbo_stream.erb, a single turbo_stream.replace targets the same dom_id and re-renders the very same _feature_flag partial. Because the partial is authoritative, the server never has to hand-build divergent HTML for the updated state; it just replays the partial with the new record. A turbo_stream.update on a shared flash region gives lightweight confirmation.
The FeatureFlagsController uses update! (bang) so a validation failure raises rather than silently rendering a stale toggle — the surrounding rescue_from (implied) can then flash an error. A subtle pitfall this design avoids is state drift: rendering from one partial means the label, the class, and the next-action URL always agree with the persisted value. This pattern is ideal for admin dashboards with many small stateful controls where full reloads feel heavy but a full SPA is overkill.
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.