erb

erb
<div data-controller="autofocus">
  <%= render 'form', post: @post %>
</div>

Autofocus first input when a Turbo modal opens (Stimulus)

rails hotwire stimulus
by Henry Kim 2 tabs
ruby
class PostsController < ApplicationController
  def create
    @post = current_member.posts.build(post_params)

    if @post.save
      flash.now[:notice] = 'Post created.'

Turbo Stream flash messages without custom JS

rails hotwire turbo
by Henry Kim 2 tabs
erb
<time data-controller="time-ago" datetime="<%= post.created_at.iso8601 %>">
  <%= post.created_at.to_fs(:short) %>
</time>

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

rails hotwire stimulus
by Henry Kim 2 tabs
ruby
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :author, class_name: "User"

  validates :body, presence: true

Model broadcasts: prepend on create, replace on update

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

export default class extends Controller {
  static targets = ["field"]

  validateField(event) {

Form validation with Stimulus and server-side errors

stimulus forms validation
by Jordan Lee 2 tabs
erb
<div id="<%= dom_id(post, :likes_counter) %>" class="text-sm text-gray-600">
  <%= post.likes.count %> likes
</div>

Live counter updates with Turbo Streams (likes, votes)

rails hotwire turbo
by Henry Kim 2 tabs
ruby
class ProjectsController < ApplicationController
  def create
    @project = Project.new(project_params)

    if @project.save
      response.set_header('Turbo-Location', project_url(@project))

Turbo-Location header: redirect a frame submission to a new URL

rails hotwire turbo
by Henry Kim 3 tabs
ruby
class LikesController < ApplicationController
  before_action :set_post

  def create
    @like = @post.likes.find_or_create_by(user: current_user)

Turbo Streams: optimistic UI for likes with disable-on-submit

rails turbo stimulus
by codesnips 3 tabs