erb ruby 61 lines · 4 tabs

Admin “quick toggle” with Turbo Streams and a single partial

Shared by codesnips Jan 2026
4 tabs
<%= 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 %>
4 files · erb, ruby Explain with highlit

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

ruby
class CommentsController < ApplicationController
  before_action :set_post

  def create
    @comment = @post.comments.build(comment_params)

System test: asserting Turbo Stream responses

rails hotwire turbo
by codesnips 4 tabs
ruby
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

rails activerecord patterns
by Alex Kumar 1 tab
ruby
module Api
  module V1
    class UsersController < BaseController
      def show
        user = User.includes(:profile).find(params[:id])

ETags for conditional requests and caching

rails caching http-caching
by Alex Kumar 1 tab
ruby
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

rails turbo hotwire
by codesnips 4 tabs
ruby
require "csv"

class PeopleCsvStream
  include Enumerable

  HEADERS = %w[id full_name email signed_up_at plan].freeze

Resilient CSV Export as a Streamed Response

rails performance streaming
by codesnips 3 tabs
erb
<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)

rails hotwire stimulus
by Henry Kim 2 tabs

Share this code

Here's the card — post it anywhere.

Admin “quick toggle” with Turbo Streams and a single partial — share card
Link copied