Maya Patel
Jan 2026
2 tabs
class PostsController < ApplicationController
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = 'Post was successfully created.'
redirect_to @post
else
flash.now[:alert] = 'Failed to create post.'
render :new, status: :unprocessable_entity
end
end
def destroy
@post = current_user.posts.find(params[:id])
@post.destroy
respond_to do |format|
format.html do
flash[:notice] = 'Post was successfully deleted.'
redirect_to posts_url
end
format.json do
render json: { message: 'Post was successfully deleted.' }, status: :ok
end
end
end
end
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
</head>
<body>
<% flash.each do |type, message| %>
<div class="alert alert-<%= type %>">
<%= message %>
</div>
<% end %>
<%= yield %>
</body>
</html>
2 files · ruby, erb
Explain with highlit
Flash messages provide one-time notifications across requests, perfect for confirming actions or showing errors. I use flash[:notice] for success messages and flash[:alert] for errors. Flash persists in session storage for one request, then auto-deletes. For AJAX requests, I return flash messages in JSON responses since redirects don't happen. The flash hash supports any keys—I add :warning and :success for semantic variety. Flash messages display in layouts, typically near the top of pages. For APIs serving React, I skip flash entirely and send messages in response bodies. Flash works beautifully for traditional request/response but needs adaptation for SPAs.
Related snips
ruby
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
System test: asserting Turbo Stream responses
rails
hotwire
turbo
by codesnips
4 tabs
ruby
class Post < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :comments, dependent: :destroy
scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.current) }
scope :draft, -> { where(published_at: nil) }
ActiveRecord scopes for reusable query logic
rails
activerecord
patterns
by Alex Kumar
1 tab
ruby
module Api
module V1
class UsersController < BaseController
def show
user = User.includes(:profile).find(params[:id])
ETags for conditional requests and caching
rails
caching
http-caching
by Alex Kumar
1 tab
ruby
class PostsController < ApplicationController
def index
@posts = Post.includes(:author)
.order(created_at: :desc)
.page(params[:page])
.per(10)
Turbo Frames: infinite scroll with lazy-loading frame
rails
turbo
hotwire
by codesnips
4 tabs
ruby
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
rails
performance
streaming
by codesnips
3 tabs
erb
<form data-controller="query-sync" data-action="change->query-sync#apply">
<select name="status" class="rounded border p-2">
<option value="">Any</option>
<option value="open">Open</option>
<option value="closed">Closed</option>
</select>
Filter UI that syncs query params via Stimulus (no front-end router)
rails
hotwire
stimulus
by Henry Kim
2 tabs
Share this code
Here's the card — post it anywhere.