Recall & Review
beginner
What is a before filter in Rails controllers?
A before filter is a method that runs before a controller action. It can prepare data or check conditions before the main action runs.
Click to reveal answer
beginner
What does an after filter do in Rails?
An after filter runs after a controller action finishes. It can be used to clean up, log information, or modify the response.
Click to reveal answer
beginner
How do you declare a before filter in Rails 5 and later?
Use
before_action :method_name inside the controller to run method_name before actions.Click to reveal answer
intermediate
Can you skip a before filter for some actions in Rails? How?
Yes, use
skip_before_action :method_name, only: [:action1, :action2] to skip the filter for specific actions.Click to reveal answer
intermediate
Why use before and after filters instead of putting code directly in actions?
Filters help keep controller actions clean and focused. They let you reuse code like authentication or logging across many actions easily.Click to reveal answer
Which method runs before a Rails controller action?
✗ Incorrect
The before_action method runs a filter before the controller action.
How do you skip a before filter for only the 'show' action?
✗ Incorrect
Use skip_before_action with only option to skip the filter for specific actions.
What is a common use for after filters?
✗ Incorrect
After filters often handle tasks like logging or cleanup after the main action.
Which Rails filter runs both before and after an action?
✗ Incorrect
around_action wraps the action, running code before and after it.
Why are filters useful in Rails controllers?
✗ Incorrect
Filters let you run shared code like authentication before actions, keeping code organized.
Explain what before and after filters do in Rails controllers and give an example use case for each.
Think about what you want to do before or after a page loads.
You got /4 concepts.
How do you apply and skip a before filter for certain actions in a Rails controller?
Look at how to control which actions run the filter.
You got /3 concepts.