Hotwire plus Ruby on Rails

๐Ÿš€ Hotwire + Ruby on Rails: The Ultimate Duo for Fast & Reactive Apps (Without JS Overload!) โค๏ธโ€๐Ÿ”ฅ๐Ÿ’ป

If youโ€™re a Rails developer looking to make your app blazing fast, interactive, and modern โ€” without turning it into a full-blown SPA โ€” Hotwire is your magic wand ๐Ÿช„. Created by Basecamp (the makers of Rails), Hotwire helps you build real-time, dynamic UIs using HTML over the wire โ€” NOT JSON. That means: less JavaScript, fewer bugs, faster apps. โšก

ChatGPT Image Dec 26, 2025, 09_16_38 PM


๐ŸŽฏ What is Hotwire? (Quick Glimpse)

Hotwire = HTML โ†’ Browser via the Cable โ€” Not JavaScript Rendering

๐Ÿ”— Hotwire is a collection of three parts:

Component Purpose
Turbo ๐Ÿš… Replaces AJAX, WebSockets & client-side rendering
Turbo Frames ๐Ÿงฑ Only update parts of the page
Turbo Streams ๐Ÿ“ก Live updates through WebSockets
Stimulus.js โšก Only JS you write โ€” lightweight behavior

๐Ÿง  Why Use Hotwire Instead of React/Vue?

Hotwire lets you:

  • Build SPA-like experiences using Rails views
  • Skip writing complex JS frameworks ๐Ÿคฏ
  • Reduce bundle size & faster loading โšก
  • Use Rails for rendering โ€” server side is king ๐Ÿ‘‘

Perfect for: dashboards, chats, notifications, e-commerce carts, live comments, admin panels.


๐Ÿ”ฅ Ways to Integrate Hotwire in Rails (With Examples)

1๏ธโƒฃ Basic Hotwire Installation ๐Ÿ› ๏ธ

rails new myapp --javascript=importmap --css=tailwind
bin/rails hotwire:install

This installs: โœ” Turbo โœ” Stimulus โœ” WebSocket + Turbo Streams support


2๏ธโƒฃ Turbo Drive โ€“ Make Rails Fast Without Code ๐Ÿš€

Turbo Drive automatically:

  • Intercepts link clicks ๐Ÿ–ฑ๏ธ
  • Replaces full-page reloads with snappy navigation
  • Keeps JS & CSS persistent in memory

Example: A standard Rails link becomes SPA-like

<%= link_to "Products", products_path %>

๐Ÿ‘‰ Loads instantly using Turbo โ€” no config needed!


3๏ธโƒฃ Turbo Frames โ€” Update Only a Portion of Page ๐Ÿงฉ

Turbo Frames let you reload partials instead of entire pages, saving bandwidth & time.

๐Ÿ’ก Example โ€” Inline Edit Form โœ๏ธ

<!-- show.html.erb -->
<turbo-frame id="user_1">
  <%= render @user %>
</turbo-frame>

<!-- _user.html.erb -->
<p><%= user.name %></p>
<%= link_to "Edit", edit_user_path(user), data: { turbo_frame: "user_1" } %>

โžก๏ธ Clicking edit only updates the frame, NOT entire page.


4๏ธโƒฃ Turbo Streams โ€” Real-Time Updates ๐Ÿ“จโšก

Want live comments or orders updating instantly? Turbo Streams use ActionCable + Rails broadcast.

๐Ÿ—ฃ Example โ€” Live Comments

Model

class Comment < ApplicationRecord
  after_create_commit { broadcast_append_to "comments" }
end

View

<turbo-stream-from "comments"></turbo-stream-from>
<div id="comments"></div>

โžก๏ธ Whenever a comment is created โ€” all users see it LIVE ๐Ÿ“ก


5๏ธโƒฃ Stimulus.js โ€” Tiny JS for Behavior ๐ŸŽฎ

Hotwire still allows small behavior-enhancing JS โ€” but no DOM rendering.

Example โ€” Auto-scrolling chat window

// controllers/scroll_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  connect() {
    this.element.scrollTop = this.element.scrollHeight
  }
}
<div data-controller="scroll">
  <%= render @messages %>
</div>

6๏ธโƒฃ Turbo + Forms = No AJAX Required ๐Ÿ“

Form responses update the DOM automatically.

๐Ÿ’ฌ Example โ€” Create Post (Turbo handles response)

<%= form_with model: @post, data: { turbo_frame: "posts" } %>

Controller

def create
  @post = Post.new(post_params)
  if @post.save
    respond_to do |format|
      format.turbo_stream
      format.html { redirect_to posts_path }
    end
  end
end

create.turbo_stream.erb

<turbo-stream action="append" target="posts">
  <%= render @post %>
</turbo-stream>

๐Ÿ† Principles for Best Use of Hotwire

Principle Meaning
๐ŸŽฏ Server Should Stay Smart Use Rails rendering โ€” avoid pushing logic to JS
๐Ÿ”„ Partial Reloading Turbo Frames instead of full reloads
๐Ÿงฉ Stream-Only Where Needed Turbo Streams for live sections only
๐Ÿ’ก Behavior โ‰  Rendering Stimulus only handles UI behavior โ€” NOT HTML building
๐ŸŒ Think HTML First Send HTML โ€” not JSON

๐Ÿฅ‡ Best Practices to Build Pro-Level Hotwire Apps

โœจ Use Broadcasting for dashboards showing live metrics โœจ Cache views using turbo_frame_cache_key โœจ Use StimulusReflex or CableReady for heavy-real-time apps โœจ Build reusable Turbo components โ†’ partials + streams โœจ Test Turbo with system tests using Capybara


๐Ÿ’ผ Real-World Use Cases

App Feature How Hotwire Helps
Live Stock Dashboard ๐Ÿ“ˆ Turbo Streams broadcast-append-replace
E-commerce Cart ๐Ÿ›’ Turbo Frames for quick add/remove
Chat System ๐Ÿ’ฌ Streams + Stimulus auto-scroll
Admin CRUD Panels โš™๏ธ Inline edit using Turbo Frames

๐ŸŽฏ Final Thoughts

Hotwire allows Rails lovers to stay Railsy โค๏ธ, writing fewer files, less JS, faster delivery, and real-time UX that competes with React โ€” with 10% complexity.

If youโ€™re building a product in 2025, this combo is the sweet spot: productivity + performance โšก

© Lakhveer Singh Rajput - Blogs. All Rights Reserved.