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. โก
๐ฏ 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.