What Are Filters?
What Are Filters?
Filters in Ruby on Rails are methods that are executed before, after, or around a controller action. They allow you to perform common tasks across multiple actions in a controller, such as authentication, authorization, logging, or setting up data for the view.
Filters are defined using the following methods in a controller:
before_action
: Executes the specified filter method before the action is executed.after_action
: Executes the specified filter method after the action is executed.around_action
: Executes the specified filter method around the action, allowing you to perform tasks before and after the action is executed.Here's an example of how to use filters in a controller:
class PostsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
before_action :set_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all
...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào