Controller Actions in Ruby
From the Ruby on Rails cheat sheet · Controllers · verified Jul 2026
Controller Actions
Common controller patterns and responses
ruby
class PostsController < ApplicationController
def index
@posts = Post.all
render json: @posts
end
def show
@post = Post.find(params[:id])
render :show
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: 'Created!'
else
render :new, status: :unprocessable_entity
end
end
end✅ Use before_action for common setup code
💡 Strong parameters required for mass assignment
🔍 respond_to handles multiple formats
⚡ Use includes() to avoid N+1 queries
controllersactionscrud