Rails 控制器生命周期 [英] Rails controller lifecycle

查看:37
本文介绍了Rails 控制器生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解一下 Rails 中的对象生命周期,我可以指望每个请求都有新实例吗?说每个 Web 请求都会创建一个新的控制器实例是真的吗?(默认情况下)

I want to know a little about the object lifecyle in rails, can I count on new instance per request?, Is it true to say that each web request creates a new controller instance?(by default)

推荐答案

是的,您可以为每个请求依赖一个新的控制器实例.

Yes, you can rely on a fresh controller instance per request.

RailsGuides:

当您的应用程序收到请求时,路由将确定运行哪个控制器和动作,然后 Rails 创建一个实例该控制器并运行与操作同名的方法.

When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the method with the same name as the action.

这篇关于Rails 控制器生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆