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 / application-pages-and-routing

Application pages and routing

The Rails page layer gives the tutorial one set of URLs for comparing Rails views, React views, server rendering, and live comment flows. Routes select page or comment actions, controllers prepare the data and rendering mode, and layouts place each result inside the shared application shell.

Route map

The route map separates the tutorial's page comparisons from its comment examples. The root page is the main React Router demonstration, while direct page routes make each rendering style easy to inspect in isolation.

URLRails targetDemonstration
/PagesController#indexMain page with the routed React comment experience
/react-router/*allPagesController#indexClient-side React Router paths that still need a Rails fallback
/simplePagesController#simpleSmall client-rendered React example
/no-routerPagesController#no_routerReact and Redux example without React Router
/server-componentsPagesController#server_componentsReact components streamed from the server
/rescriptPagesController#rescriptReScript rendering example
/stimulusCommentsController#stimulusStimulus and Rails comment page
/horizontal-form, /stacked-form, /inline-formCommentsController form actionsTurbo frame form variants
/comment-listCommentsController#comment_listTurbo frame comment-list refresh
/comments and /comments/:idResourceful comment actionsClassic Rails create, read, update, and delete flow

The rsc_payload_route and /cable mount support the server-component and live-update demonstrations. They are supporting endpoints rather than user-facing page choices. The React Router wildcard is important because Rails must send unknown client-side paths to the same page action instead of treating them as missing Rails routes.

flowchart TD
    Browser[Browser request] --> Routes[Rails route map]
    Routes --> Pages[PagesController]
    Routes --> Comments[CommentsController]
    Pages --> PageViews[Page templates]
    Comments --> CommentViews[Comment templates and partials]
    PageViews --> Shell[Application shell]
    CommentViews --> StimulusShell[Stimulus shell]

References: config/routes.rb• L3-27

Page rendering paths

The page controller exposes several front-end arrangements without changing the Rails entry point. Each action chooses the amount of server data and the rendering mode that the related template needs.

Main tutorial page

The root page prepares comments in descending ID order and gives the React application data for its initial view. It also enables the router-aware Redux store, so the page can demonstrate server rendering followed by browser-side continuation.

The template presents the tutorial links and mounts RouterApp. The /react-router/*all route uses this same action, which lets React Router own paths below the page while Rails remains the initial request handler. See React rendering modes for the client and server boundary.

References: app/…/pages_controller.rb• L6-35 app/…/index.html.erb• L176-205 config/routes.rb• L11-19

Rendering variants

The direct page routes make the main rendering choices visible as separate pages:

  • /simple mounts a small React component without preloaded data or server rendering.
  • /no-router supplies the comments store and mounts the React comment app without a router.
  • /server-components selects the latest ten comments and streams a server-rendered React page.
  • /rescript mounts the prerendered ReScript example.

These pages use the normal application layout. The distinction is the page template's rendering contract, not a separate Rails application or route tree.

References: app/…/pages_controller.rb• L28-46 app/…/simple.html.erb• L1-16 app/…/no_router.html.erb• L1-6 app/…/server_components.html.erb• L1-5 app/…/rescript.html.erb• L1

Comment demonstration paths

The comment controller serves two related experiences. Resourceful actions show the standard Rails page cycle, while named demonstration actions return the pieces used by the Stimulus and Turbo flow.

Classic Rails comment flow

The /comments resource is the baseline Rails form flow. The index page lists comments and links to show, edit, delete, or create actions. New and edit pages share one form, and successful mutations return the user to the relevant resource page with a notice.

The same actions can answer JSON requests. This gives the React demonstrations a Rails-backed comment data shape without changing the meaning of the standard HTML actions. Comment experience covers the comment behavior and live-update concerns in more detail.

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

Stimulus and Turbo flow

The /stimulus page combines a Stimulus controller boundary with a server-rendered form and comment list. The form variant routes provide three focused partial responses: horizontal, stacked, and inline. The comment-list route provides a focused response for refreshing the displayed list.

Each interactive partial stays inside a named Turbo frame. This lets a form or list update without replacing the full page. The page also keeps the three form choices visible through shared tabs, so the routes act as a small comparison tool rather than separate products.

sequenceDiagram
    participant User
    participant Page as Stimulus page
    participant Rails as CommentsController
    participant Frame as Turbo frame

    User->>Page: Open /stimulus
    Page->>Rails: Request comments and new form
    Rails-->>Page: HTML shell with form and list
    User->>Frame: Choose form or submit comment
    Frame->>Rails: Request named form or comment action
    Rails-->>Frame: Frame HTML or validation result
    Page->>Rails: Refresh comment list
    Rails-->>Frame: Updated list HTML

References: config/routes.rb• L21-25 app/…/comments_controller.rb• L71-99 app/…/stimulus.html.erb• L1-20 app/…/_tabs.html.erb• L1-5 app/…/_horizontal_form.html.erb• L1-23 app/…/_stacked_form.html.erb• L1-22 app/…/_inline_form.html.erb• L1-22 app/…/_comment_list.html.erb• L1-12

Layouts and templates

Layouts define the page shell around the selected template. The shell keeps navigation, footer, asset loading, security metadata, and the page content in one place while allowing the tutorial pages to focus on their chosen rendering example.

Application shell

The application layout wraps the main page routes and resourceful comment pages. It renders the React navigation and footer around the current template, loads the shared browser assets, and exposes Redux store hydration data for pages that send initial state from Rails.

The page templates are therefore content slots inside a common Rails document. A change to this layout can affect every rendering demonstration even when the route and controller remain unchanged.

References: app/…/application.html.erb• L1-33 app/…/index.html.erb• L1-4 app/…/no_router.html.erb• L1-6

Stimulus shell

The comment controller selects stimulus_layout for all of its actions. This shell keeps the same navigation and footer pattern but provides the asset and document setup used by the Stimulus and Turbo examples. Its content slot receives either a full comment page or a resourceful comment template.

The shared page header is a partial. The main page and Stimulus page use it to expose common tutorial links without coupling their primary templates to each other.

References: app/…/comments_controller.rb• L3-7 app/…/stimulus_layout.html.erb• L1-27 app/…/_header.html.erb• L1-78 app/…/stimulus.html.erb• L3-19

Comment template set

The comment templates divide responsibilities by response size. Full templates describe complete pages, the classic form partial is shared by new and edit pages, and Turbo partials return only the frame content that needs to change.

Comment text is converted from Markdown to safe HTML when list views display it. The show page keeps the raw text presentation used by the classic resource view. This difference lets the tutorial compare a basic Rails resource page with the richer interactive list.

References: app/…/index.html.erb• L21-36 app/…/show.html.erb• L1-18 app/…/_form.html.erb• L1-25 app/…/_comment_list.html.erb• L1-12 app/…/comments_helper.rb• L3-10

Change checklist

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

  • When a page URL changes, update both the Rails route and any links or client-side route assumptions that reach it.
  • When a page changes its initial React data or rendering mode, keep the controller preparation and template mount contract aligned.
  • When a Turbo form or list frame changes its identifier, update the matching partial response and the Stimulus refresh target together.
  • When a controller changes its layout choice, check that its asset bundle and shared shell still support the page's browser behavior.
  • When comment JSON shape changes, check the React stores and the Jbuilder output used by the page actions.

Important failure modes

TriggerConsequenceGuard
A React Router path has no wildcard Rails fallbackDirect navigation to that client path returns a Rails route errorKeep the wildcard route mapped to the main page action
A page controller stops preparing the data expected by its templateServer rendering or browser hydration starts with missing comments or store stateTreat each controller action and its template mount as one rendering contract
A Turbo response uses a different frame ID from the requesting pageThe browser cannot replace the intended form or comment listKeep frame names consistent across form, list, and refresh links
A comment mutation receives invalid dataThe full page or Turbo frame must show validation errors instead of the new commentPreserve the separate HTML error paths and the shared error display
The application layout omits Redux hydration dataReact pages that rely on server-provided store state cannot continue with the same initial stateKeep hydration output in the shared layout when store-backed pages use it

Key files

FileWhy to read it
config/routes.rbMaps public URLs to page, comment, support, and fallback endpoints.
app/…/pages_controller.rbShows how each page route selects data and a rendering mode.
app/…/comments_controller.rbDefines the classic resource flow and the focused Turbo responses.
app/views/layouts/application.html.erbDefines the shared shell for the main rendering demonstrations.
app/views/layouts/stimulus_layout.html.erbDefines the shell used by the Stimulus and comment routes.
app/views/pages/index.html.erbShows the main tutorial page and its React Router mount.
app/views/comments/stimulus.html.erbShows how the Stimulus page combines the form and list partials.
app/views/comments/turbo/_horizontal_form.html.erbIllustrates the Turbo frame contract for one form variant.
app/views/comments/turbo/_comment_list.html.erbIllustrates the focused list response used by refresh behavior.