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

middle

What Things We Can Define In The Model?

middle

How does Ruby on Rails use the Model View Controller (MVC) framework?

middle

Explain what is Interpolation in Ruby?

Bình luận

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

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