Introducing a new way to review your PRs.

Read the announcement

shakacode / react-webpack-rails-tutorial

Legacy full-app React on Rails tutorial repo and live demo. See reactonrails.com/examples for current maintained demos.

View on GitHub

16 documents · Updated Nov 4, 2026

docs / turbo-stimulus-comments

Turbo and Stimulus comments UI

The Turbo and Stimulus comments UI is the Hotwire example for viewing and posting comments through a Rails page. Rails supplies the page and HTML fragments, Turbo swaps those fragments without a full page visit, and Stimulus adds form behavior and live updates. It is one of the comment implementations described in Comment experience implementations, alongside the Rails comment backend, React comments UI, realtime comment updates, and ReScript comment demo.

Interface scope

Stimulus comments page

The /stimulus page joins one Stimulus controller to two Turbo frames: a form area and a comment-list area. It presents the horizontal form first and gives the engineer a small view of client checks, Markdown text, manual refresh, comment expiry, and updates from another browser. The page uses the shared Stimulus layout, so the Hotwire assets load with the surrounding application shell. References: app/views/comments/stimulus.html.erb#L1-L20, app/views/layouts/stimulus_layout.html.erb#L1-L27, config/routes.rb• L21-27

Form variants

The form area supports horizontal, stacked, and inline layouts without changing the comment contract. Each variant posts the same author and text values, keeps the same validation and reset behavior, and is returned as an HTML fragment when its tab is selected. Turbo replaces only the form_tabs frame, which lets the layout change while the current comment list stays in place. References: app/views/comments/turbo/_horizontal_form.html.erb#L1-L23, app/views/comments/turbo/_stacked_form.html.erb#L1-L22, app/views/comments/turbo/_inline_form.html.erb#L1-L22, app/views/comments/turbo/_tabs.html.erb#L1-L5, app/…/comments_controller.rb• L81-99

Turbo interaction

Fragment navigation

Turbo treats the form and list as separate update areas. A tab link requests a server-rendered form fragment and replaces the form frame. A refresh link requests the current comment list and replaces the list frame. This keeps the page shell and the other frame stable while Rails remains the source of the displayed HTML. References: app/views/comments/turbo/_horizontal_form.html.erb#L1-L4, app/views/comments/turbo/_comment_list.html.erb#L1-L4, app/…/comments_controller.rb• L75-99, config/routes.rb• L22-25

Submission and refresh flow

The submit flow sends the form to the normal comments resource. After Turbo finishes the request, Stimulus checks the two fields, reports missing values in the page, or clears only the text field and requests a fresh list. The author field stays populated so a person can post several comments without entering the name again.

The following diagram shows how a post moves between the browser and the Rails comment resource.

sequenceDiagram
    participant Person
    participant Stimulus as Stimulus controller
    participant Turbo
    participant Rails
    participant List as comment-list frame

    Person->>Turbo: Submit author and text
    Turbo->>Rails: POST /comments
    Rails-->>Turbo: Turbo form response
    Turbo-->>Stimulus: turbo:submit-end
    alt Both fields have values
        Stimulus->>List: Click hidden refresh link
        List->>Rails: GET /comment-list
        Rails-->>List: Rendered comment-list frame
    else A field is empty
        Stimulus-->>Person: Show field errors
    end

The browser-side check improves feedback, while the Rails model remains the authority for whether a comment can be saved. A failed server save therefore still depends on the server response and its validation rules, not only on the client check. References: client/…/comments_controller.js• L14-47, app/views/comments/turbo/_horizontal_form.html.erb#L7-L22, app/…/comments_controller.rb• L23-45, app/…/comment.rb• L3-5

Comment list rendering

The list frame displays comments in newest-first order and renders comment text as safe HTML derived from Markdown. A manual refresh uses the same server path as the post-success refresh, so a reload reflects the current database state instead of relying only on browser state. References: app/views/comments/turbo/_comment_list.html.erb#L1-L11, app/…/comments_controller.rb• L75-79, app/…/comments_controller.rb• L102-120, app/…/comments_helper.rb• L3-10

Stimulus behavior

Controller targets

The comments Stimulus controller defines targets for the list, the two inputs, the hidden refresh link, and the validation message. The HTML partials connect these targets to the controller through data attributes, which gives the controller one stable contract across all three form layouts. References: client/…/comments_controller.js• L11-13, app/views/comments/turbo/_horizontal_form.html.erb#L7-L16, app/views/comments/turbo/_error_notice.html.erb#L1-L5

Validation feedback

The controller gives immediate feedback when the author or text is blank. It names each missing field in the alert area and leaves the entered values available for correction. When both values exist, it hides the alert, clears old messages, clears the text, and starts the list refresh. References: client/…/comments_controller.js• L14-41, app/views/comments/turbo/_error_notice.html.erb#L1-L5

Controller registration

Stimulus starts with the client controller bundle and discovers JavaScript controllers from the controller directory. The page layout loads that bundle, while the data-controller="comments" marker activates the comments controller for the page. References: client/…/index.js• L1-6, client/…/stimulus-bundle.js• L1-12, app/views/comments/stimulus.html.erb#L5-L5

Live comment delivery

Action Cable subscription

When the comments controller connects, it subscribes to the shared CommentsChannel. The channel exposes the comments stream, and the controller inserts each received comment at the top of the visible list. This path lets a second open browser see a saved comment without a manual refresh. References: client/…/comments_controller.js• L48-71, app/…/comments_channel.rb• L1-7

Comment relay

A committed comment is handed to the relay job, which broadcasts it to the shared stream unless the record was destroyed before delivery. The Turbo/Stimulus page consumes this event directly, while the broader delivery path is covered in Realtime comment updates. References: app/…/comment.rb• L3-5, app/…/comment_relay_job.rb• L1-7, app/…/comments_channel.rb• L3-6

Markdown display paths

Stored comments use server-rendered Markdown when the list frame is loaded or refreshed. A live event is converted to HTML in the browser before insertion. Both paths aim to show the same comment content, but their render paths are separate and must stay aligned when Markdown behavior changes. References: app/…/comments_helper.rb• L4-10, app/views/comments/turbo/_comment_list.html.erb#L5-L9, client/…/comments_controller.js• L60-64

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the shared form_tabs and comment_list frame IDs aligned between the form partials, the links, and the controller fragment responses.
  • Keep Stimulus target names aligned between comments_controller.js and every form or list partial. A missing target can disable reset, refresh, or validation feedback without changing the Rails request contract.
  • Keep the Action Cable stream name aligned across the browser subscription, CommentsChannel, and CommentRelayJob so saved comments still reach open pages.
  • Review both server Markdown rendering and live browser rendering when the comment text format or sanitization rules change.
  • Preserve the Turbo submit-end hook when changing form markup. It connects the server request result to the client reset and refresh flow.

Important failure modes

TriggerConsequenceGuard
A form or list loses its expected Stimulus targetThe page cannot show validation feedback, clear text, refresh the list, or insert live comments through that targetKeep target names in the partials and controller synchronized
The form frame ID or fragment route changes on one side onlyTab changes can render a full page or update the wrong areaTreat form_tabs and the form fragment routes as one contract
The comment stream name differs between the channel and relayComments save but open Stimulus pages do not receive live updatesUse the same comments stream name in all three realtime participants
The browser and server Markdown paths divergeA new comment can look different before and after the next refreshApply compatible parsing and sanitization rules to both display paths
A server validation rule changes without matching the client messageThe form can pass the client check but fail on the server with unclear feedbackKeep client checks as convenience only and verify the Rails validation response

Key files

FileWhy to read it
app/views/comments/stimulus.html.erbShows the page-level composition of the controller, form, list, and user-facing behavior.
client/…/comments_controller.jsDefines validation, Turbo refresh behavior, and live comment insertion.
app/views/comments/turbo/_horizontal_form.html.erbShows the shared form contract and the Turbo submit hook.
app/views/comments/turbo/_stacked_form.html.erbShows the stacked presentation of the same comment contract.
app/views/comments/turbo/_inline_form.html.erbShows the inline presentation of the same comment contract.
app/views/comments/turbo/_comment_list.html.erbDefines the list frame, refresh link, and server-rendered comment output.
app/…/comments_controller.rbConnects page and fragment routes to comment creation and list loading.
app/…/comments_channel.rbDefines the shared realtime stream consumed by the page.
app/…/comment_relay_job.rbConnects committed comments to the realtime stream.