Câu hỏi phỏng vấn Ruby on Rails
Câu hỏi

What Are Filters?

Câu trả lời

What Are Filters in Ruby on Rails?

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:

ruby Copy
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

middle

Gợi ý câu hỏi phỏng vấn

junior

Explain what is the role of sub-directory app/controllers and app/helpers?

middle

What exactly are Harnesses and Fixtures in the Ruby?

senior

What Is The Flash?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào