ruby

ruby
Rails.application.configure do
  # Bullet configuration
  config.after_initialize do
    Bullet.enable = true
    Bullet.alert = true # Show JavaScript alert
    Bullet.bullet_logger = true # Log to bullet.log

Rails N+1 query detection with Bullet

rails performance n-1
by Maya Patel 3 tabs
ruby
class UserRegistrationForm
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :email, :string
  attribute :name, :string

Form objects for complex form handling

ruby rails form-objects
by Sarah Mitchell 2 tabs
ruby
class PostsController < ApplicationController
  def new
    @post = Post.new
  end

  def create

Inline markdown preview using Turbo Frames

rails hotwire turbo
by codesnips 4 tabs
erb
<%= form_with url: autosave_draft_path,
              method: :post,
              local: false,
              data: {
                controller: "autosave",
                autosave_target: "form",

Autosave drafts with Stimulus + Turbo (lightweight)

rails hotwire stimulus
by codesnips 3 tabs
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
<!DOCTYPE html>
<html>
  <head>
    <title>Contacts</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

Turbo Frames modal: load, submit, then close via stream

rails turbo hotwire
by codesnips 4 tabs
ruby
module RetryableTransaction
  module_function

  RETRIABLE = [ActiveRecord::Deadlocked, ActiveRecord::LockWaitTimeout].freeze

  def run(max_retries: 3, base_delay: 0.05, &block)

Deadlock-Aware Retry Wrapper

rails postgres reliability
by codesnips 3 tabs
ruby
class Post < ApplicationRecord
  # View counter - increments without hitting the database
  kredis_counter :view_count, expires_in: 1.day

  # Recent viewers list - stores last 10 viewer IDs
  kredis_unique_list :recent_viewers, limit: 10

Rails Kredis for higher-level Redis operations

rails redis kredis
by Maya Patel 2 tabs
ruby
module Idempotency
  extend ActiveSupport::Concern

  included do
    before_action :check_idempotency_key, only: [:create, :update]
    after_action :store_idempotent_response, only: [:create, :update]

Request deduplication with idempotency keys

rails api reliability
by Alex Kumar 1 tab
ruby
base_path = Rails.root.join('storage', 'exports').realpath
requested = base_path.join(params[:filename].to_s).cleanpath

unless requested.to_s.start_with?(base_path.to_s) && requested.file?
  raise ActionController::RoutingError, 'Not Found'
end

Preventing path traversal in download endpoints

path-traversal file-security secure-coding
by Kai Nakamura 1 tab
ruby
# Simple mixin
module Loggable
  def log(message)
    puts "[#{Time.now}] #{self.class.name}: #{message}"
  end
end

Module mixins and concerns for code reuse

ruby modules mixins
by Sarah Mitchell 3 tabs
ruby
module Api
  module V1
    class PostsController < ApplicationController
      def create
        post = current_user.posts.build(post_params)

Rails strong parameters for nested attributes

rails security strong-parameters
by Maya Patel 1 tab