Skip to the workspace
Pebellslearn by building
Meet Rails  ›  Capstone Project

Rails and the Frontend: Hotwire

Rails & Frontend 15 minRails sends HTML from the server, and Hotwire (Turbo plus Stimulus) makes those pages feel fast and interactive without writing a separate JavaScript app.

ruby on rails / Rails and the Frontend: Hotwire

Explore each scene at your own pace.

01 · The rule

The rule

Rails and the Frontend: Hotwire

Many modern sites are two programs: a backend that sends JSON and a JavaScript app that builds the page. Rails can work that way, but its default is simpler. The server sends ready-made HTML, and a set of tools called Hotwire makes those pages feel as quick as a single-page app.

The rule

Send HTML over the wire; add JavaScript only where HTML cannot reach.

Hotwire is included in every new Rails app. It has two main parts:

  • Turbo — speeds up server-rendered HTML.
    • Turbo Drive: intercepts link clicks and form submissions, fetches the new page in the background and swaps the <body>. No full reload, no code from you.
    • Turbo Frames: wrap part of a page in <turbo-frame id="..."> so a link or form inside it replaces only that part.
    • Turbo Streams: the server sends small instructions such as append, replace or remove with a piece of HTML, over a normal response or a WebSocket.
  • Stimulus — small JavaScript controllers attached to existing HTML with data-controller attributes, for behaviour that needs no server, like toggling or copying.
<turbo-frame id="reviews">
  <%= render @book.reviews %>
</turbo-frame>
<div data-controller="expand">
  <button data-action="expand#toggle">Show more</button>
</div>

If you need a separate React or mobile client instead, rails new bookclub --api builds a JSON-only backend with no views.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.