ruby
class Recipe < ApplicationRecord
  has_many :ingredients, inverse_of: :recipe, dependent: :destroy

  accepts_nested_attributes_for :ingredients,
    allow_destroy: true,
    reject_if: :all_blank

Add/remove nested fields with Stimulus (no cocoon)

rails hotwire stimulus
by codesnips 4 tabs
erb
<%# Subscribe every viewer of this post to its broadcasts %>
<%= turbo_stream_from @post %>

<%# Update the acting user's button to reflect the toggle %>
<%= turbo_stream.replace dom_id(@post, :like_button) do %>
  <%= render "posts/like_button", post: @post %>

Live counter updates with Turbo Streams (likes, votes)

rails hotwire turbo
by codesnips 4 tabs
erb
<div class="bookmarks">
  <h1>Saved Bookmarks</h1>

  <table>
    <tbody id="bookmarks">
      <%= render partial: "bookmark", collection: @bookmarks %>

Link that submits DELETE with Turbo (data-turbo-method)

rails hotwire turbo
by codesnips 4 tabs
ruby
module TurboResponder
  extend ActiveSupport::Concern

  private

  def respond_with_turbo(resource, on_success:, partial:, notice: nil)

A reusable respond_to block for turbo_stream + html

rails hotwire turbo
by codesnips 3 tabs
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
ruby
module LayoutlessTurboFrame
  extend ActiveSupport::Concern

  included do
    layout :layout_for_turbo
  end

Render without layout for Turbo Frame requests

rails hotwire turbo
by codesnips 4 tabs
ruby
module TimeAgoHelper
  def time_ago_tag(time, locale: I18n.locale, **options)
    return if time.blank?

    time = time.to_time
    fallback = l(time, format: :short)

Time-ago formatting with Stimulus (no heavy date libs)

rails hotwire stimulus
by codesnips 3 tabs
erb
<!DOCTYPE html>
<html>
  <head>
    <title><%= content_for(:title) || "App" %></title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

Importmap setup for Stimulus controllers (Rails 7 style)

rails hotwire stimulus
by codesnips 4 tabs
erb
<%= form_with model: @document, data: { controller: "upload" } do |form| %>
  <div class="field">
    <%= form.label :file, "Attach a document" %>
    <%= form.file_field :file,
          direct_upload: true,
          data: { upload_target: "input" } %>

Active Storage direct upload progress with Stimulus

rails hotwire stimulus
by codesnips 3 tabs
ruby
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :author, class_name: "User"

  has_rich_text :body

Action Text comment form that works inside a Turbo Frame

rails hotwire turbo
by codesnips 3 tabs
erb
<%= turbo_stream_from [current_member, :notifications] %>

<div id="notification_badge">
  <%= render 'notifications/badge', count: current_member.notifications.unread.count %>
</div>

Notifications dropdown that live-updates with Turbo Streams

rails hotwire turbo
by Henry Kim 2 tabs
ruby
class TasksController < ApplicationController
  def destroy
    @task = Task.find(params[:id])
    @task.destroy!

    respond_to do |format|

Remove deleted items instantly with turbo_stream.remove

rails hotwire turbo
by Henry Kim 2 tabs