erb ruby 63 lines · 3 tabs

Turbo Frames: Inline edit that swaps form <-> row

Shared by codesnips Jan 2026
3 tabs
<%= turbo_frame_tag dom_id(contact) do %>
  <tr class="contact-row">
    <td><%= contact.name %></td>
    <td><%= contact.email %></td>
    <td><%= contact.role.titleize %></td>
    <td class="actions">
      <%= link_to "Edit", edit_contact_path(contact), class: "btn btn-sm" %>
      <%= link_to "View", contact_path(contact),
                  data: { turbo_frame: "_top" }, class: "btn btn-sm btn-ghost" %>
    </td>
  </tr>
<% end %>
3 files · erb, ruby Explain with highlit

Inline editing lets a user click an "Edit" link on a table row and have that single row transform into an edit form in place, without a full page reload or a modal. This snippet shows the canonical Hotwire approach using a Turbo Frame that wraps each row so only that row is swapped when the frame's contents are replaced.

The key idea is that a <turbo-frame> with a stable id acts as an addressable region: any navigation or form submission originating inside the frame targets only that frame, and Turbo replaces the frame's contents with the matching frame from the response. In _contact.html.erb, each row is wrapped in turbo_frame_tag dom_id(contact), which produces a unique id like contact_42. The "Edit" link navigates to edit_contact_path, and because that link lives inside the frame, Turbo fetches the edit page and extracts the frame with the same id from it.

That matching frame is defined in _form.html.erb, again using turbo_frame_tag dom_id(contact). When the edit response comes back, only the row's frame swaps from a read-only display into the form. The "Cancel" link points back at the contact's show/frame path so the row can be restored without submitting anything.

ContactsController ties it together. The edit action just renders the row partial containing the form frame. On update, the successful branch re-renders _contact — Turbo sees a frame with the same id and swaps the form back into the static row, giving the illusion of an in-place save. The failure branch re-renders _form with status: :unprocessable_entity so validation errors appear inside the frame; returning a non-2xx status is important because Turbo will still process the frame but signals the error.

The trade-off is that each row becomes an independent navigation context, so links must explicitly break out with data-turbo-frame or target: "_top" when they should affect the whole page. This pattern is ideal for editable tables and admin CRUD screens where a modal would be heavy and a full reload would lose scroll position and context. It degrades gracefully: without JavaScript, the frame links behave as ordinary full-page navigations.


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.

Turbo Frames: Inline edit that swaps form <-> row — share card
Link copied