Filters & Callbacks in Ruby

From the Ruby on Rails cheat sheet · Controllers · verified Jul 2026

Filters & Callbacks

Before/after/around filters for controller actions

ruby
class ApplicationController < ActionController::Base
  before_action :authenticate_user!
  before_action :set_locale
  after_action :log_action
  around_action :catch_errors

  private

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end
end
✅ before_action runs before controller actions
💡 Use only/except to limit filter scope
🔍 skip_before_action skips inherited filters
⚡ around_action wraps action execution
controllersfilterscallbacks

More Ruby tasks

Back to the full Ruby on Rails cheat sheet