What Are Filters? And How Many Ty...
What Are Filters? And How Many Ty...
In Ruby on Rails, filters are methods that run at specific points during the request-response cycle of a web application. They allow developers to execute code before, after, or around a controller action, providing a way to manage tasks such as authentication, logging, and performance monitoring.
Before Filters:
class ApplicationController < ActionController::Base
before_action :authenticate_user
private
def authenticate_user
redirect_to login_path unless current_user
end
end
After Filters:
class ApplicationController < ActionController::Base
after_action :log_request
private
def log_request
Rails.logger.info("Request processed for #{request.path}")
end
end
Around Filters:
class ApplicationController < ActionController::Base
around_action :measure_time
private
def measure_time
...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào