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 / comment-experience

Comment experience implementations

The comment experience is a shared tutorial feature that lets a visitor create and view comments through several Rails and JavaScript interfaces. Each interface uses the same comment data and server contract while showing a different way to combine Rails HTML, JSON requests, React state, Turbo frames, Stimulus behavior, ReScript, and Action Cable updates.

Shared comment capability

The comment capability centers on a small record with an author and text. Rails validates both values, presents comments newest first, renders Markdown for HTML views, and removes comments older than one day through the daily maintenance task. The feature is a comparison surface: the user-facing result stays similar while the delivery path changes.

Comment data and validation

The shared record is the contract between every interface. HTML and JSON clients submit author and text; JSON responses also expose the comment id and time fields. Blank author or text values fail at the Rails boundary, so each client must keep its error state aligned with that validation.

References: app/…/comment.rb• L3-5 db/…/20140823052830_create_comments.rb• L1-8 app/…/_comment.json.jbuilder• L1 lib/…/daily.rake• L1-10

Comment request and update flow

The server supports both a normal Rails resource flow and lightweight requests for JavaScript clients. A successful create or update changes the shared record and can produce either an HTML response or a JSON comment; a committed change also enters the realtime path.

The main flow is:

flowchart LR
  U[Visitor] --> V{Interface}
  V --> R[React UI]
  V --> T[Turbo and Stimulus UI]
  V --> S[ReScript demo]
  V --> C[Classic Rails pages]
  R --> E[Comments HTML and JSON endpoints]
  T --> E
  S --> E
  C --> E
  E --> M[Comment record]
  M --> J[Committed change relay]
  J --> A[Action Cable comments stream]
  A --> R
  A --> T
  A --> S

References: config/routes.rb• L21-27 app/…/comments_controller.rb• L23-69 app/…/comment.rb• L3-5 app/…/comment_relay_job.rb• L3-6 app/…/comments_channel.rb• L3-6

Realtime comment delivery

After a comment transaction commits, a background relay broadcasts the comment on the comments stream unless the record has been destroyed. React, Stimulus, and ReScript demos subscribe to that stream and place received data into their local display state, which lets two open browsers see new activity without a manual reload.

Realtime delivery does not replace the initial list. React and ReScript load current comments from the JSON endpoint before they listen for later events; the Turbo and Stimulus page starts with a server-rendered list. A manual refresh remains useful when a connection is down or when a record was removed by maintenance.

See Realtime comment updates for the server relay and client subscription boundary.

References: app/…/comment_relay_job.rb• L3-6 app/…/comments_channel.rb• L3-6 client/…/CommentBox.jsx• L56-91 client/…/comments_controller.js• L48-71 client/…/ReScriptShow.res• L39-69

Interface variants

The application exposes the same comment task through interfaces that have different ownership of rendering and state. The route determines whether Rails returns a complete page, a Turbo fragment, or a React mount point; the underlying comment resource remains shared.

React comment interfaces

The main React experience combines a form, a comment list, loading and save feedback, locale selection, and Action Cable updates. Its Redux-backed route receives initial comments from Rails and can also fetch the JSON list; the simpler React route keeps the same form and list behavior in component state without Redux or realtime subscription.

The React form offers horizontal, stacked, and inline layouts. The list keeps comments in newest-first order and renders Markdown as sanitized HTML. This makes the React route useful for studying both a full shared-state flow and a smaller client-only flow against the same Rails API.

See React comments user interface for the React containers, state flow, routing, and browser requests.

References: app/…/pages_controller.rb• L6-35 app/…/index.html.erb• L180-204 app/…/no_router.html.erb• L1-6 app/…/simple.html.erb• L1-16 client/…/CommentBox.jsx• L33-91 client/…/CommentForm.jsx• L22-111

Turbo and Stimulus comment interface

The Turbo and Stimulus route keeps the page mostly in Rails HTML. Turbo frames replace the form or comment list without a full page load, while the Stimulus controller checks the two required fields, resets the text field after a submit, requests a fresh list, and listens for realtime comments.

The form has three server-rendered layouts: horizontal, stacked, and inline. The tabs switch which partial is loaded into the form frame, so layout changes do not create a second comment workflow.

See Turbo and Stimulus comments UI for the frame targets, controller behavior, partials, and server interactions.

References: config/routes.rb• L21-25 app/…/comments_controller.rb• L71-99 app/…/stimulus.html.erb• L5-19 app/…/_horizontal_form.html.erb• L1-23 app/…/_comment_list.html.erb• L1-12 client/…/comments_controller.js• L11-71

ReScript comment interface

The ReScript route presents a typed React version of the same feature. It fetches the JSON list when the page starts, posts a comment with the Rails authenticity token, tracks fetch and save status, and subscribes to the comments channel.

Its form also switches between horizontal, inline, and stacked layouts. The demo keeps its own typed state and uses small bindings for HTTP requests, Markdown conversion, and Action Cable. This route shows that a different client language can use the same server contract without changing the Rails resource.

See ReScript comment demo for the typed state, JavaScript bridges, generated component wrapper, and build path.

References: config/routes.rb• L14-15 app/…/rescript.html.erb• L1 client/…/ReScriptShow.res• L22-95 client/…/Actions.res• L1-59 client/…/RescriptShow.jsx• L1-8

Classic Rails comment interface

The classic Rails route is the baseline for the feature. It lists comments in a table and supports create, show, edit, and destroy actions through normal HTML requests. Its views show validation messages and success notices without a client-side comment store.

The same controller can answer JSON requests for the JavaScript routes. This keeps persistence, validation, and response data in one server API while the classic page demonstrates the direct Rails version of the workflow.

See Rails comment backend for persistence, controller actions, response formats, and Rails views.

References: config/routes.rb• L26 app/…/comments_controller.rb• L9-69 app/…/index.html.erb• L1-37 app/…/_form.html.erb• L1-25

Cross-interface contracts

The implementations can share behavior only when their small set of contracts stays stable. The most important contracts are the comment fields, the JSON envelope, the Markdown display rule, and the Action Cable stream name.

Client and server boundaries

The React and ReScript clients expect GET comments.json to return an object with a comments array. Each array item needs an integer id, an author, and text; React also receives the time fields from the JSON partial even when the list does not display them. New comments use the comment request key when posted.

The Rails and React renderers do not share one Markdown function. Rails uses its helper, and the main React list sanitizes converted HTML. The Stimulus and ReScript demos convert Markdown in their own client paths. A change to accepted Markdown or input trust therefore needs review across every display route.

References: app/…/index.json.jbuilder• L1-2 app/…/_comment.json.jbuilder• L1 client/…/requestsManager.js• L4-33 client/…/Actions.res• L27-59 app/…/comments_helper.rb• L3-10 client/…/Comment.jsx• L4-31

Page and store coordination

The main page seeds the React Router store with server data so the first render has the current comment list. The no-router React page uses a separate store with the same comment state shape, while the simple page starts empty and loads after mount. The navigation bar can read the shared store to show the current comment count on the Redux-backed routes.

This split is part of the tutorial comparison. It means a change to initial props, store names, or route selection can affect page hydration even when the comment endpoint still works.

References: app/…/pages_controller.rb• L6-35 client/…/serverRegistration.jsx• L3-21 client/…/commentsStore.js• L8-22 client/…/routerCommentsStore.js• L11-31 client/…/NavigationBarContainer.jsx• L9-29

Change checklist

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

  • Keep the comments JSON envelope and the id, author, and text fields aligned with both the React request layer and the ReScript decoder.
  • Review all Markdown display paths when changing parsing, sanitization, or allowed input. The Rails helper, React list, Stimulus controller, and ReScript list do not use the same rendering path.
  • Keep the comments Action Cable stream name aligned between the Rails channel, relay job, and each client subscriber.
  • Check the seeded Redux props and store names when changing the root, no-router, or navigation-bar mounts. These routes do not obtain their first comment list in the same way.
  • Review refresh behavior after changing comment deletion or daily cleanup. Destroyed records are not broadcast by the relay job.
  • Keep the Turbo frame names and Stimulus target names aligned when changing the form layouts or comment-list partial.

Important failure modes

TriggerConsequenceGuard
author or text is blankRails rejects the save and the client shows a save error instead of a new comment.Keep client feedback consistent with the model validation and preserve entered values on failed React submissions.
The JSON envelope or required field names changeReact or ReScript cannot build the comment list from the response.Treat comments, id, author, and text as a shared API contract.
The Action Cable stream or channel name differs between one side of the connectionNew comments still persist, but open browsers do not receive live updates.Keep the relay, channel, and subscribers on the comments stream and retain an initial fetch or manual refresh.
A comment is deleted or removed by daily cleanupA connected client can keep showing the old item until it refreshes because destroyed records are not relayed.Use the list endpoint or manual refresh as the source for current state.
Markdown behavior changes in only one rendererThe same text can display differently across Rails, React, Stimulus, and ReScript pages.Review and validate every Markdown display path together.
A Turbo frame or Stimulus target is renamed on only one sideForm replacement, validation reset, or list refresh stops working on the Turbo page.Keep frame IDs and data-comments-target names synchronized with the controller.

Key files

FileWhy to read it
app/…/comment.rbDefines the shared validation and the handoff to realtime delivery.
app/…/comments_controller.rbShows the common HTML, JSON, and Turbo-facing comment actions.
app/views/comments/index.json.jbuilderDefines the JSON list envelope used by JavaScript clients.
app/…/comment_relay_job.rbConnects committed comment changes to the realtime stream.
app/…/comments_channel.rbDefines the Action Cable stream consumed by live clients.
client/…/CommentBox.jsxShows the main React experience from initial fetch through form, list, and live updates.
client/…/CommentForm.jsxShows the shared React form layouts and submission behavior.
client/…/comments_controller.jsShows how Stimulus connects Turbo fragments to validation, refresh, and Action Cable.
app/views/comments/turbo/_comment_list.html.erbShows the Turbo frame and Stimulus targets for the live list.
client/app/bundles/comments/rescript/ReScriptShow.resShows the ReScript page lifecycle and its typed fetch, form, and realtime state.
client/app/bundles/comments/rescript/Actions/Actions.resDefines the ReScript view of the shared HTTP data contract.
app/views/pages/index.html.erbMounts the main React Router comment experience.
app/views/pages/simple.html.erbMounts the smaller React experience without Redux or server props.