rails

ruby
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :author, class_name: "User"

  validates :body, presence: true, length: { maximum: 5_000 }

Declarative model broadcasts with broadcasts_to (Rails 7)

rails hotwire turbo
by codesnips 4 tabs
ruby
Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :users, only: [:index, :show, :create, :update]
      resources :posts do
        resources :comments, only: [:index, :create]

API versioning with namespace routing

rails api versioning
by Alex Kumar 2 tabs
javascript
import { Controller } from "@hotwired/stimulus"

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

  connect() {

Stimulus: autofocus the first invalid field after Turbo update

rails stimulus hotwire
by codesnips 3 tabs
ruby
class CommentsController < ApplicationController
  before_action :set_post

  def create
    @comment = @post.comments.build(comment_params)

Turbo Streams fallback to HTML for older clients

rails hotwire turbo-streams
by codesnips 4 tabs
ruby
class WizardsController < ApplicationController
  before_action :load_draft

  def step_1
    render_step(1)
  end

Hotwire-powered multi-step forms

hotwire turbo forms
by Jordan Lee 3 tabs
ruby
module Middleware
  class RequestTimer
    def initialize(app)
      @app = app
    end

Custom middleware for request tracking

rails rack middleware
by Alex Kumar 2 tabs
ruby
module ConnectionHealth
  extend ActiveSupport::Concern

  def with_fresh_connection
    conn = ActiveRecord::Base.connection
    conn.verify! # pings and reconnects if the socket is dead

Keep DB Connections Healthy in Long Jobs

rails activerecord background-jobs
by codesnips 3 tabs
erb
<article class="post">
  <h1><%= @post.title %></h1>
  <%= simple_format @post.body %>

  <!-- Lazy load comments when scrolled into view -->
  <%= turbo_frame_tag "post_#{@post.id}_comments",

Lazy-loading Turbo Frames for performance

hotwire turbo performance
by Jordan Lee 3 tabs
ruby
class Order < ApplicationRecord
  has_many :line_items, dependent: :destroy, inverse_of: :order

  accepts_nested_attributes_for :line_items,
    reject_if: :all_blank,
    allow_destroy: true

Transactionally Create Parent + Children with accepts_nested_attributes_for

rails activerecord nested-attributes
by codesnips 3 tabs
css
.confirm-dialog {
  border: none;
  border-radius: 12px;
  padding: 1.5rem;
  max-width: 26rem;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);

Stimulus: resilient confirmation for destructive actions

rails stimulus hotwire
by codesnips 3 tabs
ruby
class Current < ActiveSupport::CurrentAttributes
  attribute :tenant, :request_id

  def tenant=(tenant)
    super
    Rails.logger.tagged("tenant=#{tenant&.id}") if tenant

Tenant Isolation in Rails with CurrentAttributes and an around_action

rails multi-tenancy current-attributes
by codesnips 4 tabs
erb
<div class="layout" data-controller="viewport">
  <%= turbo_frame_tag "master", class: "master-pane" do %>
    <h1>Products</h1>
    <ul class="product-list">
      <% @products.each do |product| %>
        <li>

Turbo Frames: conditional frame navigation for mobile vs desktop

rails hotwire responsive
by codesnips 4 tabs