简体   繁体   English

Rails:如何在视图中访问在控制器中声明的实例变量?

[英]Rails: How can an instance variable declared in a controller accessible in a view?

Ruby instance variables are accessible to a single object. Ruby实例变量可由单个对象访问。 But in rails, if I declare an instance variable in a controller, it is still accessible in the views. 但是在rails中,如果我在控制器中声明实例变量,则仍然可以在视图中访问它。 What is the architecture behind this? 这背后的架构是什么?

Well, your controller calls render which renders your templates. 好吧,您的控制器调用render来渲染您的模板。 So, the template code is being run within the scope of the controller instance. 因此,模板代码正在控制器实例的范围内运行。 Therefore, you can use any instance variables declared. 因此,您可以使用任何声明的实例变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM