Rails comment backend
The Rails comment backend is the shared data and request layer for the repository's comment examples. It stores comments, checks the two required user fields, serves HTML pages and JSON resources, and supplies the views used by the classic Rails and Turbo comment flows. The comment experience page gives the wider product view; the React comments UI, real-time comment updates, Turbo and Stimulus comments, and ReScript comment demo pages describe clients that use this backend.
Comment lifecycle
The comment lifecycle starts with a browser or client request and ends with a stored comment or a validation response. A successful write can also enter the asynchronous broadcast path used by the real-time examples.
flowchart LR
A[HTML or JSON request] --> B[Comment resource actions]
B --> C[Comment validation]
C -->|valid| D[Comments table]
C -->|invalid| E[HTML errors or JSON errors]
D --> F[HTML redirect or JSON resource]
D --> G[Asynchronous comment broadcast]
The resource actions use the same comment record for all client styles. This keeps the classic pages, the Turbo forms, and the JavaScript examples aligned on the fields they can submit and display.
References: app/…/comments_controller.rb• L9-45 app/…/comment.rb• L3-5 app/…/comment_relay_job.rb• L3-6
Comment persistence
The comment record is the backend's small, shared data contract. It stores an author name and comment text with creation and update times in the comments table.
Persistence contract
Both author and text must be present before a comment can be saved. The database also keeps these fields non-null, so the model rule and the stored data protect the same contract. The text field accepts Markdown input; the views convert it to safe HTML when they render comment lists.
References: app/…/comment.rb• L3-5 db/schema.rb• L45-50 app/…/comments_helper.rb• L3-10
Commit notification
After a record commits, the backend queues a relay for the comment stream. This separates persistence from delivery and lets the real-time clients receive a newly saved comment without changing the normal HTML or JSON response.
References: app/…/comment.rb• L3-5 app/…/comment_relay_job.rb• L3-6 app/…/comments_channel.rb• L3-6
Resource actions
CommentsController provides the standard comment resource actions and a small set of page actions for the example clients. The resource routes are declared by resources :comments, while the named example routes select a form layout or a comment list.
Standard resource actions
The standard actions cover listing, viewing, creating, editing, updating, and deleting comments. List responses order comments by descending ID, so the newest saved comment appears first. Create and update accept only author and text from the comment input.
On a failed create or update, the controller keeps the validation errors for the HTML form or returns them as an unprocessable JSON response. On a successful delete, HTML clients return to the list and JSON clients receive an empty response.
References: config/routes.rb• L21-27 app/…/comments_controller.rb• L9-69 app/…/comments_controller.rb• L102-120
Example page actions
The example actions prepare the same comment data for different presentation flows. The Stimulus page combines a form and a list, while the form actions return horizontal, stacked, or inline form partials. The comment-list action returns the list partial so a client can refresh that part of the page without loading the full page.
References: config/routes.rb• L21-26 app/…/comments_controller.rb• L71-99 app/…/stimulus.html.erb• L1-19
Response formats
The backend treats HTML and JSON as two views of the same resource contract. HTML responses support people using the example pages; JSON responses support the React, ReScript, and other client code that manages its own display.
HTML responses
Regular HTML create and update requests return the user to the saved comment with a status message. A Turbo frame create stays in the frame flow instead of using the regular redirect path. Invalid HTML submissions render the form again with the model errors, while the delete action returns to the comment list.
The HTML show and index pages expose the standard scaffold-style journey. The index page gives access to a comment, its edit form, and its delete action; the show page gives access to edit and return to the list.
References: app/…/comments_controller.rb• L23-69 app/…/index.html.erb• L5-37 app/…/show.html.erb• L1-18
JSON responses
The JSON index response wraps the ordered records under comments. Each record includes its ID, author, text, and timestamps. The JSON show and successful create or update responses use the same record shape, while validation failures return the model errors with status 422.
JSON delete responses have no body. This gives a client a clear success signal without asking it to parse a deleted resource.
References: app/…/comments_controller.rb• L9-69 app/…/index.json.jbuilder• L1-2 app/…/show.json.jbuilder• L1 app/…/_comment.json.jbuilder• L1
Rails views
The Rails views provide the server-rendered surfaces that make the comment contract visible. They share the same model but use different page shapes for the classic resource example and the Turbo and Stimulus examples.
Classic comment views
The classic views show comments in a table and provide the standard new, edit, and show pages. The shared form collects a name and Markdown text, displays validation messages, and posts through the comment resource routes. The index and Turbo list render comment text through the Markdown helper so stored input becomes readable HTML.
References: app/…/index.html.erb• L10-36 app/…/new.html.erb• L1-5 app/…/edit.html.erb• L1-6 app/…/_form.html.erb• L1-25 app/…/show.html.erb• L6-18
Turbo comment views
The Turbo views split the comment experience into a form frame and a list frame. The three form partials change only the layout of the same author and text inputs; the tabs select which layout the user sees. The list partial loads the newest comments and exposes a refresh target for the Stimulus client.
This arrangement lets the example pages replace a small part of the document after a request. It also keeps the server response useful to clients that do not use React.
References: app/…/_horizontal_form.html.erb• L1-23 app/…/_stacked_form.html.erb• L1-22 app/…/_inline_form.html.erb• L1-22 app/…/_tabs.html.erb• L1-5 app/…/_comment_list.html.erb• L1-12
Markdown rendering
Comment text is entered as Markdown and rendered as sanitized HTML in list views. The show view presents the stored text as plain output instead, so the display depends on which Rails view a user opens.
References: app/…/comments_helper.rb• L3-10 app/…/index.html.erb• L21-25 app/…/_comment_list.html.erb• L4-9 app/…/show.html.erb• L11-14
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the model validation, database null rules, and controller parameter contract aligned for
authorandtext. - Keep the JSON partial shape aligned with clients that consume index, show, create, and update responses.
- When changing the list order or list partial, check both the full HTML index and the partial refresh path.
- When changing create or update responses, check both regular HTML requests and Turbo frame requests.
- When changing commit behavior, check the Action Cable stream and the clients that expect newly saved comments.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A create or update omits author or text | The record is not saved; HTML shows form errors and JSON returns 422. | Keep model validation and the database non-null contract in place. |
| A client expects a different JSON field set than the shared partial provides | The client cannot build the same comment view from index and show responses. | Treat the JSON partial as the resource shape for every JSON action. |
| A Turbo form receives the regular redirect path | The response can leave the frame flow instead of replacing the intended form or list area. | Preserve the controller's separate Turbo frame and regular HTML branches. |
| A comment is deleted after its save broadcast is queued | The relay skips delivery for the destroyed record, so a client may need its own refresh to remove stale display data. | Use the list refresh path when a client needs authoritative current data. |
Key files
| File | Why to read it |
|---|---|
| app/…/comments_controller.rb | Defines the shared resource actions, response branches, list ordering, and example endpoints. |
| app/…/comment.rb | Defines the comment validity rule and the handoff to the broadcast path. |
| db/schema.rb | Shows the stored comment fields and database constraints. |
| config/routes.rb | Maps standard resource requests and the named example page requests. |
app/views/comments/_form.html.erb | Shows the classic HTML input and validation surface. |
app/views/comments/index.html.erb | Shows the full HTML list and its resource actions. |
app/views/comments/index.json.jbuilder | Shows the collection-level JSON response contract. |
app/views/comments/_comment.json.jbuilder | Shows the shared JSON record fields. |
app/views/comments/turbo/_comment_list.html.erb | Shows the partial list used by the Turbo and Stimulus examples. |
app/views/comments/turbo/_horizontal_form.html.erb | Shows the shared form behavior in one of the three Turbo layouts. |
| app/…/comments_helper.rb | Defines the safe Markdown-to-HTML display path. |