Helper 方法不会更新 Controllers 实例变量

发布于 2024-11-09 13:42:37 字数 825 浏览 2 评论 0原文

我在页面控制器中定义了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小鸟爱天空丶 2024-11-16 13:42:37

实例变量在对象之间复制,而不是共享。在视图中,会创建具有相同名称的新变量,并“指向”控制器中变量引用的同一对象。因此,如果您为视图中的变量分配新值,其他对象将不会知道该更改。

但是,如果您确实需要更改某个变量引用的对象,请修改该对象,而不是将新对象分配给变量。一个例子会告诉你我的意思。

@var = "a new string" # it creates a new String object.
@var.replace "a modified string" # it changes the content of the object.

如果您使用“替换”方法(字符串对象具有此类方法),那么您的控制器可能会看到更改。如果您只是将一个新对象分配给一个变量,您的控制器将看不到更改。但不要太相信我,因为我通常不会修改视图中的对象。测试一下吧。

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.

@var = "a new string" # it creates a new String object.
@var.replace "a modified string" # it changes the content of the object.

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.

痞味浪人 2024-11-16 13:42:37

我也对此感到好奇,所以我想看看“处理顺序”。这是该测试的结果。它为我澄清了事情......希望它能回答问题,即使不能解决问题。所有视图均在 haml 中。请原谅无关的代码,即%br。我的目的是为了清晰而不是简洁。

控制器:

@test = 1

助手:

def change_test
  @test += 1
end

应用程序布局:

!!!
%html
  %body
    Application Layout Pre-change = 
    = @test
    %br
    Application Layout Change = 
    = change_test()
    %br    
    = yield

生成视图:

%br
Pre-Header = 
= @test
%br
Pre Header change = 
= change_test()
%br
= render "test_header"
%br
Yeilded page Pre-change = 
=@test 
%br
Yeilded Page Change = 
= change_test()
%br
= render "test_footer"
%br

标题部分:

%br
Header Pre-change = 
= @test
%br
Header change = 
= change_test()
%br

页脚部分:

%br
Footer Pre-change = 
= @test
%br
Footer change = 
= change_test()
%br

输出:

Application Layout Pre-change = 5 
Application Layout Change = 6 

Pre-Header = 1 
Pre Header change = 2 

Header Pre-change = 2 
Header change = 3 

Yeilded page Pre-change = 3 
Yeilded Page Change = 4 

Footer Pre-change = 4 
Footer change = 5 

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:

@test = 1

Helper:

def change_test
  @test += 1
end

Application Layout:

!!!
%html
  %body
    Application Layout Pre-change = 
    = @test
    %br
    Application Layout Change = 
    = change_test()
    %br    
    = yield

Yielded View:

%br
Pre-Header = 
= @test
%br
Pre Header change = 
= change_test()
%br
= render "test_header"
%br
Yeilded page Pre-change = 
=@test 
%br
Yeilded Page Change = 
= change_test()
%br
= render "test_footer"
%br

Header Partial:

%br
Header Pre-change = 
= @test
%br
Header change = 
= change_test()
%br

Footer Partial:

%br
Footer Pre-change = 
= @test
%br
Footer change = 
= change_test()
%br

Output:

Application Layout Pre-change = 5 
Application Layout Change = 6 

Pre-Header = 1 
Pre Header change = 2 

Header Pre-change = 2 
Header change = 3 

Yeilded page Pre-change = 3 
Yeilded Page Change = 4 

Footer Pre-change = 4 
Footer change = 5 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文