erb

ruby
class SubscriptionsController < ApplicationController
  before_action :set_subscription, only: %i[show destroy]

  def new
    @subscription = Subscription.new
  end

Use 303 See Other after POST in Turbo flows

rails hotwire turbo
by codesnips 3 tabs
javascript
import * as Turbo from "@hotwired/turbo";

function metaContent(name) {
  const el = document.querySelector(`meta[name="${name}"]`);
  return el ? el.getAttribute("content") : null;
}

Attach custom headers to Turbo fetch requests (stimulus-free)

rails hotwire turbo
by codesnips 3 tabs
erb
<h1>Create your account</h1>

<%= render "error_summary", record: @user %>

<%= form_with model: @user, url: signups_path do |f| %>
  <div class="field">

Update an error summary area via turbo_stream.update

rails hotwire turbo
by codesnips 3 tabs
erb
<%# Fragment caching - caches rendered HTML %>
<% cache @product do %>
  <h1><%= @product.name %></h1>
  <p><%= @product.description %></p>
  <p>Price: $<%= @product.price %></p>
<% end %>

Rails caching strategies for performance

ruby rails caching
by Sarah Mitchell 4 tabs
erb
<div class="dashboard">
  <header class="dashboard__header">
    <h1>Overview</h1>
    <p class="muted">Real-time metrics update as they load.</p>
  </header>

Server-rendered skeleton UI for slow frames

rails hotwire turbo
by codesnips 4 tabs
ruby
class ArticlesController < ApplicationController
  before_action :set_article, only: %i[edit update]

  def new
    @article = Article.new
  end

Turbo-friendly 422 responses for invalid forms

rails hotwire turbo
by codesnips 3 tabs
erb
<%= form_with model: @profile do |form| %>
  <div
    class="field"
    data-controller="character-count"
    data-character-count-max-value="280"
  >

Character counter for textareas with Stimulus

rails hotwire stimulus
by codesnips 3 tabs
erb
<h1>Catalog</h1>

<%= turbo_frame_tag "products" do %>
  <div class="product-grid" data-controller="frame">
    <% @products.each do |product| %>
      <%= render "products/card", product: product %>

Pagination links that escape a Turbo Frame with _top

rails hotwire turbo
by codesnips 4 tabs
erb
<%= tag.div id: dom_id(comment), class: "comment", data: { controller: "comment" } do %>
  <div class="comment__body">
    <%= comment.body %>
  </div>

  <footer class="comment__meta">

Use dom_id everywhere to keep Turbo replacements deterministic

rails hotwire turbo
by codesnips 4 tabs
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["sentinel"]
  static values = {
    url: String

Infinite scroll with Turbo Frames and Intersection Observer

hotwire turbo stimulus
by Jordan Lee 3 tabs
ruby
class WizardsController < ApplicationController
  STEPS = %w[account profile confirm].freeze

  before_action :set_step

  def show

Multi-step wizard navigation with Turbo Frames

rails hotwire turbo
by codesnips 4 tabs
erb
<%= turbo_stream.prepend 'items' do %>
  <%= render @item %>
<% end %>

<turbo-stream action="reset_form" target="new_item_form"></turbo-stream>

Custom Turbo Stream action: reset a form after success

rails hotwire turbo
by Henry Kim 3 tabs