Helper 方法不会更新 Controllers 实例变量
我在页面控制器中定义了一个 instance_variable 并使用一些字符串对其进行初始化。
我将该 instance_variable 包含在页面中。
它出现了。
伟大的!
如果我的页面包含一些 _header 布局,该布局使用更新该 instance_variable 的 Pages Helper 方法,则我的页面显示原始字符串而不是更新后的字符串。
日志显示 _header 在页面之前呈现,因此它确实调用了 Pagers Helper 方法,该方法在呈现我的页面之前更新该 instance_variable 。
那么为什么该页面不包含更新后的字符串呢?
我对 RoR 很陌生,试图了解它是如何工作的。
提前致谢!
编辑:
嗯。即使 Rails 服务器日志显示,yield 编辑页面已在 渲染 编辑页面之后渲染...看起来它已在 之前< /em> 他们。
如果我在第一个render编辑页面中更改instance_variable,则更改后的值在所有后续render编辑页面中可用,但未更改在< strong>yield 编辑页面,即使 yield 位于 render 之间(在 application.html.erb 中),并且 Rails 服务器日志甚至显示已作为最后渲染。
编辑:
出于某种原因,我使用“布局”一词,而我必须使用“部分”一词。
I define an instance_variable in my Pages Controller and initialize it with some string.
I include that instance_variable in a page.
It shows up.
Great!
If my page includes some _header layout, which uses a Pages Helper method which updates that instance_variable, my page shows the original and not the updated string.
Logs show that the _header was rendered before the page, so it did called a Pagers Helper method that updates that instance_variable BEFORE it has rendered my page.
So why does that page not include the updated string?
Im new to RoR, trying to understand how it works.
Thanks in advance!
EDIT:
Well. Even if the rails server log show, that the yield ed page has been rendered after the render ed pages... It looks like it has been rendered before them.
If I change an instance_variable in the first render ed page, the changed value is available in all following render ed pages, but unchanged in the yield ed page, even if the yield lays inbetween render 's(in application.html.erb), and the rails server logs show even that is has been render as the last.
EDIT:
For some reason I user the word 'layouts' where I would had to use the word 'partials'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实例变量在对象之间复制,而不是共享。在视图中,会创建具有相同名称的新变量,并“指向”控制器中变量引用的同一对象。因此,如果您为视图中的变量分配新值,其他对象将不会知道该更改。
但是,如果您确实需要更改某个变量引用的对象,请修改该对象,而不是将新对象分配给变量。一个例子会告诉你我的意思。
如果您使用“替换”方法(字符串对象具有此类方法),那么您的控制器可能会看到更改。如果您只是将一个新对象分配给一个变量,您的控制器将看不到更改。但不要太相信我,因为我通常不会修改视图中的对象。测试一下吧。
The instance variables are copied, not shared, between objects. In a view a new variables are created with the same names, and 'pointing' to the same object which was referenced by the variables in the controller. So, if you assign a new value to a variable in a view, other objects will not know about the change.
But if you really need to change an object referenced by some variable, modify that object instead of assigning a new one to a variable. An example will tell you what I mean.
If you use a 'replace' method (String objects have such method) then your controller may see the change. If you just assign a new object to a variable, your controller will not see the change. But don't trust me too much, as I usually don't modify the objects in a view. Just test it.
我也对此感到好奇,所以我想看看“处理顺序”。这是该测试的结果。它为我澄清了事情......希望它能回答问题,即使不能解决问题。所有视图均在 haml 中。请原谅无关的代码,即%br。我的目的是为了清晰而不是简洁。
控制器:
助手:
应用程序布局:
生成视图:
标题部分:
页脚部分:
输出:
I was curious about this as well, so I wanted to see the "order of processing". This is the result of that test. It clarified things for me... hopefully it answers the question even if doesn't solve the problem. All views are in haml. Pardon the extraneous code i.e. %br. I was going for clarity over brevity.
Controller:
Helper:
Application Layout:
Yielded View:
Header Partial:
Footer Partial:
Output: