0
0
Ruby on Railsframework~30 mins

Before and after filters in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Before and After Filters in Rails Controllers
📖 Scenario: You are building a simple Rails web app to manage a list of books. You want to run some code automatically before and after certain actions in your controller to prepare data and log activity.
🎯 Goal: Create a Rails controller called BooksController that uses before_action and after_action filters. The before_action will set a variable with a welcome message before the index action. The after_action will log a message after the index action runs.
📋 What You'll Learn
Create a BooksController class inheriting from ApplicationController
Add a before_action filter named set_welcome_message that runs only before the index action
Add an after_action filter named log_index_access that runs only after the index action
Define the index action that sets an instance variable @books to an array of book titles
Define the set_welcome_message method that sets @welcome_message to the string "Welcome to the Books List!"
Define the log_index_access method that calls Rails.logger.info with the message "Index action was accessed"
💡 Why This Matters
🌍 Real World
Before and after filters are used in Rails apps to run code automatically around controller actions. For example, setting up data, checking permissions, or logging activity without repeating code in every action.
💼 Career
Understanding filters is important for Rails developers to write clean, maintainable controllers and implement common behaviors like authentication, logging, and data preparation efficiently.
Progress0 / 4 steps
1
Create the BooksController with the index action
Create a class called BooksController that inherits from ApplicationController. Inside it, define a method called index that sets an instance variable @books to the array ["The Hobbit", "1984", "Brave New World"].
Ruby on Rails
Need a hint?

Remember to use def to define the index method and set @books inside it.

2
Add a before_action filter to set a welcome message
Add a before_action filter called set_welcome_message that runs only before the index action. Then define the set_welcome_message method that sets @welcome_message to the string "Welcome to the Books List!".
Ruby on Rails
Need a hint?

Use before_action :set_welcome_message, only: [:index] and define the method below the index action.

3
Add an after_action filter to log access
Add an after_action filter called log_index_access that runs only after the index action. Then define the log_index_access method that calls Rails.logger.info with the message "Index action was accessed".
Ruby on Rails
Need a hint?

Use after_action :log_index_access, only: [:index] and define the method that logs the message.

4
Complete the controller with all filters and actions
Ensure the BooksController class includes the before_action filter set_welcome_message for index, the after_action filter log_index_access for index, and defines the methods index, set_welcome_message, and log_index_access exactly as specified.
Ruby on Rails
Need a hint?

Review all previous steps and ensure the controller has all filters and methods exactly as specified.