ruby
39 lines · 1 tab
Maya Patel
Jan 2026
1 tab
module Api
module V1
class PostsController < ApplicationController
def create
post = current_user.posts.build(post_params)
if post.save
render json: post, serializer: PostSerializer, status: :created
else
render json: { errors: post.errors }, status: :unprocessable_entity
end
end
def update
post = current_user.posts.find(params[:id])
if post.update(post_params)
render json: post, serializer: PostSerializer
else
render json: { errors: post.errors }, status: :unprocessable_entity
end
end
private
def post_params
params.require(:post).permit(
:title,
:body,
:status,
:cover_image_blob_id,
tags: [],
images_attributes: [:id, :blob_id, :caption, :_destroy],
meta_attributes: [:description, :keywords, :og_image]
)
end
end
end
end
1 file · ruby
Explain with highlit
Strong parameters prevent mass assignment vulnerabilities by explicitly permitting allowed attributes. For nested associations like a post with embedded images or comments, I use nested permit calls. Arrays of primitives use [] syntax, while hashes of attributes use nested hashes. The _destroy parameter enables deletion of associated records through forms. I create private permit methods for complex nested structures to keep controllers clean. Strong parameters work seamlessly with React forms that post JSON—Rails parses the JSON into params automatically. Validation errors for nested attributes surface correctly in API responses. This security layer is essential for any Rails API accepting user input.
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
payload = {
sub: user.id,
iss: 'https://auth.example.com',
aud: 'codesnips-api',
exp: 15.minutes.from_now.to_i,
iat: Time.now.to_i,
JWT issuance and verification without common footguns
jwt
authentication
api
by Kai Nakamura
2 tabs
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
Share this code
Here's the card — post it anywhere.