0
0
Ruby on Railsframework~10 mins

Before and after filters in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Before and after filters
Request received
Run before filters
Run controller action
Run after filters
Send response
Rails runs before filters first, then the action, then after filters before sending the response.
Execution Sample
Ruby on Rails
class PostsController < ApplicationController
  before_action :authenticate_user
  after_action :log_action

  def show
    # show post
  end
end
This code runs authenticate_user before show action, then runs log_action after it.
Execution Table
StepFilter/ActionWhen RunEffectNotes
1authenticate_userBefore actionChecks user loginPrevents action if not logged in
2showActionDisplays postMain controller method
3log_actionAfter actionLogs action detailsRuns after action, before response sent
4Response sentAfter filters doneSends page to userEnd of request cycle
💡 Request ends after after_action filters run and response is sent
Variable Tracker
VariableStartAfter before_actionAfter actionAfter after_actionFinal
user_authenticatedniltrue or falsetrue or falsetrue or falsetrue or false
response_readyfalsefalsetruetruetrue
log_writtenfalsefalsefalsetruetrue
Key Moments - 3 Insights
Why does the before_action run before the controller method?
Because Rails runs all before_action filters first to prepare or check things before the main action runs, as shown in execution_table step 1 before step 2.
Can after_action filters change the response sent to the user?
Yes, after_action filters run after the action but before the response is sent, so they can modify the response (e.g., headers), mainly for logging or cleanup, as shown in execution_table step 3 before step 4.
What happens if a before_action filter stops the request?
If a before_action filter halts the request (like redirecting), the controller action and after_action filters do not run, stopping at that filter.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the main controller action run?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Filter/Action' column for the controller action name.
According to variable_tracker, when does 'log_written' become true?
AAfter after_action
BAfter action
CAfter before_action
DAt start
💡 Hint
Look at the 'log_written' row and see when it changes to true.
If the before_action filter fails authentication, what happens next?
AController action runs anyway
BAfter_action runs but action does not
CRequest stops before action and after_action
DResponse is sent before filters
💡 Hint
Recall key_moments about before_action halting the request.
Concept Snapshot
Rails filters run in order:
- before_action: runs before controller method, used for checks
- controller action: main code for request
- after_action: runs after action, used for logging or cleanup
Filters help organize code around actions cleanly.
Full Transcript
In Rails, before_action filters run first to prepare or check things before the main controller action runs. Then the controller action executes to handle the request. After that, after_action filters run for tasks like logging. Finally, the response is sent to the user. If a before_action filter stops the request, the action and after_action filters do not run. Variables like user authentication status change during before_action, response readiness after the action, and logs are written after after_action. This flow helps keep controller code clean and organized.