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

senior

Explain the difference between Page, Action, Fragment, Low-Level, SQL caching types

middle

What is the difference between symbol and string ?

expert

What sort of problems you have faced with Ruby on Rails and how do you think the same can affect the
projects?

Bình luận

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

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