AgentSkillsCN

hotwire

借助 Hotwire(Turbo + Stimulus)构建现代化的 Rails 界面。当您需要实现以下功能时,本技能将为您提供有力支持:(1) 使用 Turbo Drive 实现页面导航与表单处理;(2) 利用 Turbo Frames 实现部分页面的动态更新;(3) 通过 Turbo Streams 实现 DOM 的实时更新;(4) 采用 Stimulus 控制器为页面添加 JavaScript 行为;(5) 通过 Action Cable 实现 WebSocket 广播;(6) 在无需依赖重型 JavaScript 框架的情况下,打造各类交互式 UI。

SKILL.md
--- frontmatter
name: hotwire
description: "Build modern Rails interfaces with Hotwire (Turbo + Stimulus). Use when implementing: (1) Page navigation and form handling with Turbo Drive, (2) Partial page updates with Turbo Frames, (3) Real-time DOM updates with Turbo Streams, (4) JavaScript behaviors with Stimulus controllers, (5) WebSocket broadcasts with Action Cable, (6) Any interactive UI without heavy JavaScript frameworks."

Hotwire

Hotwire sends HTML over the wire instead of JSON, enabling SPA-like interactivity without JavaScript frameworks.

Decision Framework

Choose the right tool for each interaction:

NeedToolExample
Fast page navigationTurbo DriveLinks, form submissions
Update one sectionTurbo FrameEdit-in-place, tabs, modals
Update multiple sectionsTurbo StreamForm creates item + clears form
Real-time updatesTurbo Stream + Action CableChat, notifications
JavaScript behaviorStimulusDropdowns, copy-to-clipboard

Rule of thumb: Use Turbo for server-driven updates, Stimulus for client-side behavior.

Turbo Frames Quick Reference

Wrap content in a frame to scope navigation:

erb
<%= turbo_frame_tag @todo do %>
  <p><%= @todo.description %></p>
  <%= link_to "Edit", edit_todo_path(@todo) %>
<% end %>

Clicking "Edit" replaces only this frame with the matching frame from the response.

Lazy loading:

erb
<%= turbo_frame_tag "sidebar", src: sidebar_path, loading: "lazy" %>

Break out of frame:

erb
<%= link_to "Full page", path, data: { turbo_frame: "_top" } %>

See references/turbo.md for complete Turbo documentation.

Turbo Streams Quick Reference

Stream actions for DOM updates:

ActionEffect
appendAdd to end of target
prependAdd to beginning
replaceReplace entire element
updateReplace innerHTML only
removeDelete element
before/afterInsert adjacent

Controller response:

ruby
respond_to do |format|
  format.turbo_stream
  format.html { redirect_to @item }
end

Template (create.turbo_stream.erb):

erb
<%= turbo_stream.append :items, @item %>
<%= turbo_stream.update :form, partial: "form" %>

Real-time broadcasts:

ruby
# In model
broadcasts_refreshes  # Simple: triggers page refresh with morphing

# Or manual:
broadcast_append_to :items, target: :items

See references/turbo.md for stream actions and broadcasting.

Stimulus Quick Reference

Controller structure:

javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["input", "output"]
  static values = { url: String, count: { type: Number, default: 0 } }
  static classes = ["active"]
  static outlets = ["other-controller"]

  connect() { /* called when controller connects to DOM */ }
  disconnect() { /* called when removed from DOM */ }

  // Action methods (called from data-action)
  submit() {
    this.outputTarget.textContent = this.inputTarget.value
    this.countValue++
    this.dispatch("submitted", { detail: { value: this.inputTarget.value } })
  }

  // Private methods
  #helperMethod() { }
}

HTML:

html
<div data-controller="example"
     data-example-url-value="/api"
     data-example-active-class="is-active">
  <input data-example-target="input" data-action="input->example#submit">
  <span data-example-target="output"></span>
</div>

Action syntax: event->controller#method

  • Default events: click for buttons, submit for forms, input for inputs
  • Modifiers: :prevent, :stop, :once, @window, @document
  • Key filters: keydown.enter->controller#method

See references/stimulus.md for complete reference.

Rails Helpers

Turbo Frame tag:

erb
<%= turbo_frame_tag @model %>
<%= turbo_frame_tag @model, :section %>
<%= turbo_frame_tag "custom_id", src: path, loading: "lazy" %>

Turbo Stream tag:

erb
<%= turbo_stream.append :target, partial: "item", locals: { item: @item } %>
<%= turbo_stream.replace @model %>
<%= turbo_stream.remove @model %>

Stimulus controller generator:

bash
bin/rails generate stimulus controller_name

See references/rails-integration.md for helpers and setup.