如何在控制器提供给意见Rails的实例变量 [英] How are instance variables in controllers made available to views in Rails

查看:137
本文介绍了如何在控制器提供给意见Rails的实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做的Rails了一会儿,刚开始深入了解Ruby的元编程它的Rails会从它的力量。我实在想不出这一个,它是让我疯了。如何在控制器实例变量提供给Rails中的观点(可欣赏共享)?我知道有一些背后的元编程的魔法,但我想不出它out.Thanks提前为您的帮助。

I have been doing Rails for a while now and just started to dive deep into Ruby meta-programming which Rails gets its power from. I really couldn't figure this one out and it is making me crazy. How are instance variables in controllers made available to views(shared with views) in Rails? I know there is some meta-programming magic behind it, but I couldn't figure it out.Thanks in advance for all your help.

推荐答案

我在下面留下它现在证明我是多么错误。够downvotes后,我决定看看这实际上是如何工作的。我原来的答案写我是很新的轨道后,是基于我怎么其他MVC库所使用的假设工作(具体是:codeIgniter)。

Update: Original accepted answer was wrong

I'm leaving it below for now to demonstrate how wrong I was. After enough downvotes I have decided to look into how this actually works. My original answer was written after I was very new to rails and was an assumption based on how other MVC libraries I had used worked (specifically: CodeIgniter).

在AbstractController (其中的ActionController :: Base的包括和你的的ApplicationController 从继承)包括一个名为方法<一href=\"https://github.com/rails/rails/blob/618d5317d3ce3d40a22293149f79ff9843bbdb35/actionpack/lib/abstract_controller/rendering.rb#L65\"相对=nofollow> view_assigns 。该方法从控制器的上下文中抓住所有的(非受保护的)的实例变量和在哈希返回它们。此方法由( ::的ActionView基地的实例<一个消耗href=\"https://github.com/rails/rails/blob/618d5317d3ce3d40a22293149f79ff9843bbdb35/actionview/lib/action_view/rendering.rb#L70\"相对=nofollow>这里)。

AbstractController (which ActionController::Base includes and your ApplicationController inherits from) includes a method called view_assigns. This method grabs all the (non-protected) instance variables from the context of your controller and returns them in a hash. This method is consumed by instances of ActionView::Base (here).

所以,基本上,实例变量是通过一点点的元编程魔术提供给您的视图由哈希。您的观点是绝对不会在你的控制器的情况下或范围执行。这就是为什么你必须显式声明的控制器方法作为辅助方法,使他们的意见可见。这个功能是通过元编程的单独的层启用。

So, basically, instance variables are provided to your view by a hash through a bit of meta-programming magic. Your view is definitely not executed in the context or scope of your controller. This is why you must explicitly declare controller methods as helper methods to make them visible to your views. That feature is enabled by separate layer of meta programming.

这不是真的那么复杂或元。在的ActionController#渲染方法执行模板code(ERB,HAML,等等)的电流范围内的的ApplicationController 实例。这意味着,所有的模板中的code是在你的的ApplicationController ,因此可以访问它的对象实例变量的方法内部执行内执行。

It's not really that complicated or "meta". The ActionController#render method executes the template code (erb, haml, whatever) within the scope of the current ApplicationController instance. That means that all of the code within your template is executed inside of a method within your ApplicationController, and as such has access to the instances variables of the object it was executed inside of.

如果你不叫渲染然后将ApplicationController中揣摩调用哪个模板渲染与基动作名。

If you don't call render then ApplicationController will try to figure out which template to call render with based on the action name.

这篇关于如何在控制器提供给意见Rails的实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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