简体   繁体   English

erb布局变量未定义且不是nil

[英]erb layout variables are undefined and not nil

What I want to do is something like variable || "default string" 我想做的是类似variable || "default string"事情。 variable || "default string" . variable || "default string"

But this works only if variable is nil and for some reason it's undefined. 但这仅在variable为nil且由于某种原因未定义时才有效。

Is it possible to set the default value to nil, or is there a better way to achieve this? 可以将默认值设置为nil,还是有更好的方法来实现呢?

您可以利用Ruby局部变量默认为nil的事实来获利:

variable ||= 'default string'

您可以这样做:

defined?(variable) ? variable : "default string"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM