Loading...
Loading...
Hotwire, Turbo, and Stimulus patterns for Rails. Use when implementing JavaScript interactions, Turbo Frames/Streams, or Stimulus controllers. Triggers on "stimulus controller", "turbo frame", "turbo stream", "hotwire", "rails javascript".
npx skill4agent add aviflombaum/claude-code-in-avinyc hotwire// app/javascript/controllers/toggle_controller.js
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["content"]
static values = { open: Boolean }
toggle() {
this.openValue = !this.openValue
}
openValueChanged() {
this.contentTarget.classList.toggle("hidden", !this.openValue)
}
}<div data-controller="toggle" data-toggle-open-value="false">
<button data-action="toggle#toggle">Toggle</button>
<div data-toggle-target="content" class="hidden">
Content here
</div>
</div><%= turbo_frame_tag "user_profile" do %>
<%= render @user %>
<% end %>
<!-- Link that updates only the frame -->
<%= link_to "Edit", edit_user_path(@user), data: { turbo_frame: "user_profile" } %># Controller
respond_to do |format|
format.turbo_stream
format.html { redirect_to @post }
end<%# app/views/posts/create.turbo_stream.erb %>
<%= turbo_stream.prepend "posts", @post %>
<%= turbo_stream.update "post_count", Post.count %>request.jsimport { get, post } from "@rails/request.js"
async function loadData() {
const response = await get("/api/data", { responseKind: "json" })
if (response.ok) {
const data = await response.json
// handle data
}
}# config/importmap.rb
pin "lodash", to: "https://ga.jspm.io/npm:lodash@4.17.21/lodash.js"RSpec.describe "Posts", type: :system do
before { driven_by(:cuprite) }
it "updates post inline with Turbo" do
post = posts(:published)
visit post_path(post)
click_link "Edit"
fill_in "Title", with: "Updated Title"
click_button "Save"
expect(page).to have_content("Updated Title")
expect(page).to have_current_path(post_path(post)) # No redirect
end
end