Rails 是否会在渲染时覆盖控制器实例变量:partial?

发布于 2024-07-26 09:21:44 字数 682 浏览 8 评论 0原文

Rails 是否会在模板中执行的 render :partial 调用中覆盖控制器实例变量? 例如,假设我们有:

控制器

def my_action
   @widget = Widget.find(params[:id])
end    

视图 my_action.html.erb

Hi there.
<%= render :partial => 'my_helper' %>
That's it, that's all.

视图 _my_helper.html.erb

The id is <%= @widget.id.to_s %>.

是否有任何记录的场景(错误或设计),其中 @widget 在使用时将为 nil我的助手? 我在应用程序中看到了这种行为,但无法追踪其来源。

一些注意事项:
* 该应用程序在 Rails 2.1.1 中运行。
* 虽然可以将局部变量分配给渲染:partial => my_helper 调用,由于多种原因,本地人在这种情况下并不理想。
* 我花了很多时间筛选 ActionController 和 ActionView,但无法隔离行为。

Are there any cases in which Rails overwrites controller instance variables on a render :partial call performed in a template? For example, let's say we have:

controller

def my_action
   @widget = Widget.find(params[:id])
end    

view my_action.html.erb

Hi there.
<%= render :partial => 'my_helper' %>
That's it, that's all.

view _my_helper.html.erb

The id is <%= @widget.id.to_s %>.

Are there any documented scenarios (bugs or by design) in which @widget would be nil when used in my_helper? I'm seeing this behavior in an application and haven't been able to track down its source.

A few notes:
* The app is running in Rails 2.1.1.
* While it would be possible to assign locals to the render :partial => my_helper call, for a number of reasons locals aren't really ideal in this case.
* I spent a good chunk of time sifting through ActionController and ActionView, but haven't been able to isolate the behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

榆西 2024-08-02 09:21:44

不,Rails 不会覆盖局部中的实例变量。

可以这样想:视图可以访问控制器的所有实例变量,并且部分变量是视图的一部分。

然而,可能存在在属于不同控制器的视图中使用共享部分的用例。 在这种情况下,部分中的代码会变得混乱,因为您可能不希望在不同控制器的方法中保持实例变量的名称相同。 在这种情况下,使用本地哈希是更好、更干净的选择

No, Rails will not overwrite the instance variables in the partial.

Think of it like this: the view has access to all the instance variables of a controller and a partial is part of the view.

There can however, be use cases where a shared partial is used in views belonging to different controller. In that case, the code within the partial becomes confusing as you might not want to keep the name of the instance variables same within the methods of different controller. Using the locals hash is a better and cleaner option in such a case

失与倦" 2024-08-02 09:21:44

这是一个有点蹩脚的建议:如果可行,请尝试使用不同的变量名称。

或者,还有旧的实用程序员“示踪子弹”想法:在控制器中创建一个虚拟变量,您可以在各种视图/片段中打印该变量,只是为了确保这些值的范围/生命周期确实是您所期望的,而不是由于其独特的名称(或您忘记编写的代码),“某物”踩踏了这个变量。

最后,您确实尝试过:

% find . -name '*rb*' | xargs grep -B 2 -A 2 -n '@widget'

或在应用程序中进行类似的代码搜索,对吗? 请注意,如果您“冻结”Rails 版本,所有 Rails 框架代码都将嵌套在您的应用程序中,您也可以搜索它。

Here's a somewhat lame suggestion: if practical, try using a different variable name.

Alternately, there's the old Pragmatic Programmer "tracer bullets" idea: create a dummy variable in the controller which you print out in various views/fragments, just to make sure that the scope/lifetime of these values really is what you expect, rather than "something" stomping on this one variable due to its unique name (or code you forgot you wrote).

Finally, you did try:

% find . -name '*rb*' | xargs grep -B 2 -A 2 -n '@widget'

or a similar code search in the application, right? Note that if you "freeze" your rails version, all of the rails framework code will be nested within you application, and you can search that, too.

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