optional local variables in rails partial templates: how do I get out of the (defined? foo) mess?

10/14 07:12
阅读数 0

问题:

I've been a bad kid and used the following syntax in my partial templates to set default values for local variables if a value wasn't explicitly defined in the :locals hash when rendering the partial -- 我是一个坏孩子,并在我的部分模板中使用以下语法来设置局部变量的默认值,如果在渲染部分时未在:locals哈希中显式定义值 -

<% foo = default_value unless (defined? foo) %>

This seemed to work fine until recently, when (for no reason I could discern) non-passed variables started behaving as if they had been defined to nil (rather than undefined). 直到最近,这似乎工作正常(当时我无法辨别)未通过的变量开始表现得好像它们被定义为nil(而不是未定义)。

As has been pointed by various helpful people on SO, http://api.rubyonrails.org/classes/ActionView/Base.html says not to use 正如SO上各种乐于助人的人所指出的, http//api.rubyonrails.org/classes/ActionView/Base.html表示使用

defined? foo

and instead to use 而是使用

local_assigns.has_key? :foo

I'm trying to amend my ways, but that means changing a lot of templates. 我正在尝试修改我的方法,但这意味着要更改很多模板。

Can/should I just charge ahead and make this change in all the templates? 可以/我应该提前充电并在所有模板中进行此更改吗? Is there any trickiness I need to watch for? 我需要注意什么诡计? How diligently do I need to test each one? 我需要多努力地测试每一个?


解决方案:

参考一: https://en.stackoom.com/question/8e2r
参考二: https://stackoom.com/question/8e2r
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部