From the course: Ruby on Rails 6: Controllers and Views

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use filters to call methods automatically

Use filters to call methods automatically - Ruby on Rails Tutorial

From the course: Ruby on Rails 6: Controllers and Views

Start my 1-month free trial

Use filters to call methods automatically

- [Narrator] In this movie, we're going to learn how to use controller filters. Controller filters execute code before or after a controller action. And they're going to do it automatically. We can use them to filter requests before allowing actions to remove code repetition, or to perform some basic housekeeping tasks. For example, you might use a controller filter to confirm that a user is logged in, before you allow them to execute any of the actions in the controller. Or you might use one to set up variables and default values, or to go to the database to find objects that you're going to need. There are three controller filters, before action, after action and around action. And they do pretty much what they say. Before action executes code inside a method, before every action in the controller. After action executes it after the action is done, and around action does it both before and after. In the last movie, we…

Contents