if 语句 Ruby on Rails
我知道这可能相当微不足道,但我已经查看了之前的问题并尝试过它们,但不幸的是它们仍然发出错误:s
我的问题如下,我有一个 html.erb 文件,我想要一个特定的正文在给定条件或其他条件下显示的文本(如果条件为假)
我有
<% if [email protected] do %>
这里有更多代码
<% end %>
我尝试了很多组合,但最常见的错误是
当你没有预料到时,你得到了一个 nil 对象! 评估 nil.nil 时发生错误
我不确定我做错了什么,这很可能很简单,但我找不到解决方法!
I know this is probably rather trivial but I have had a look at previous questions and I've tried them but they still issued an error unfortunately :s
My issue is the following, I have an html.erb file and I want a certain body text to be display given a condition or another if it is false
I have
<% if [email protected] do %>
more code goes here
<% end %>
I have tried many combinations but the most frequent error i keep getting is
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.nil
I'm not sure what I'm doing wrong, this is most likely something very easy but I can't find my way through it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有
.nil
方法。您正在考虑.nil?
。它应该是[电子邮件受保护]?
。或者,您可以从
if @selector
获得相同的结果,并且其计算结果将与if [电子邮件受保护]?
。这是假设您没有引用布尔值。There's no
.nil
method. You're thinking of.nil?
. It should be[email protected]?
.Or you could get the same result from
if @selector
and it will evaluate to be the same asif [email protected]?
. This is assuming you not referring to a boolean.也许尝试.nil?或 == nil,或者仔细查看为 @selector 赋值的控制器代码。
Maybe try .nil? or == nil, or take a close look at your controller code that's assigning a value to @selector.
就
足够了
just
is enough
在 Rails 中,更惯用的方式是 <% if @selector.present?做%>或者用惯用的 Ruby <% 除非@selector.nil?做%>
In Rails, a more idiomatic way would be <% if @selector.present? do %> or in idiomatic Ruby <% unless @selector.nil? do %>