我有一个控制器,我想总是调用after_action
before_action :auth
after_action :track, only [:index]
def index
render json: ..., status: 200
end
def show
...
end
def auth
return if some_condition
render json: ..., status 422
end
def track
status = response.status if response.successful?
# do something with status
end
确保始终调用after_action的最佳方法是什么?目前,只有在实际达到索引操作时才会调用它。
我尝试在after_action的only部分添加:auth
1条答案
按热度按时间wgeznvg71#
after_action callback只有在它所附加的action被执行时才会被调用。如果要确保无论执行哪个操作都始终调用跟踪after_action,则可以在每个操作开始时使用before_action调用它
通过添加before_action:track,track方法将在控制器中的每个动作之前被调用,确保它总是被执行。