为什么我的三元运算符代码返回两个条件?

Why is my ternary operator code returning both conditions?

在我的 Rails 应用程序中,我在创建新产品时输入的表单字段决定了显示的图像。如果我在 'verdict' 文本框中输入,这意味着该产品很糟糕,所以我会显示一个灰色的、不活动的亚马逊按钮。

如果我在 'goodverdict' 框中输入,它是合法产品,所以我们会看到亚马逊的购买按钮,它链接到产品,因为我在表格中输入了 URL。我首先尝试使用 present?,最近将其切换为 presence(如下所示)。两者的结果相同。

但是,当我只在 'goodverdict' 框中输入时,我会得到两个按钮(即好像两个参数都为真),并且会出现 "false?" 文本。

<div class="col-md-4" id="belowvid">
<%= @product.verdict.presence %> ? <%=@product.verdict %> <br> <%= image_tag('https://s3.amazonaws.com/isitcrap/amazon+gray+button.jpg', width: 275) %> :
<%= @product.goodverdict %> <br>
<%= link_to image_tag("https://s3.amazonaws.com/isitcrap/amazon-Button.png", width: 275), @product.buylink.html_safe, target: :_blank %>

这来自一个部分,该部分正在由索引呈现的另一个部分呈现。

您的三元运算符不是 运行ning,因为它不在 ruby 部分,而是在 html.

只有 <%/%> 标签内的内容会 运行 为 ruby...其他所有内容都会像 html 一样打印到屏幕上.

在这种情况下 - 您可能想要的是这样的:

<% if @product.verdict.present? %>
  <%= @product.verdict %> <br> <%= image_tag('https://s3.amazonaws.com/isitcrap/amazon+gray+button.jpg', width: 275) %>
<% else %>
 <%= @product.goodverdict %> <br>
 <%= link_to image_tag("https://s3.amazonaws.com/isitcrap/amazon-Button.png", width: 275), @product.buylink.html_safe, target: :_blank %>
<% end %>