【问题标题】:What is the cleanest way to implement if/not in ERB in Rails? [closed]在 Rails 的 ERB 中实现 if/not 的最简洁方法是什么? [关闭]
【发布时间】:2012-04-04 14:21:34
【问题描述】:

从索引视图考虑以下代码:

<td>
  <%= 'foo' if bar == BAZ %>
  <%= 'qux' if bar != BAZ %>
</td>

这是最干净的方法,还是有更好的方法?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 views erb


    【解决方案1】:

    这对于ternary conditional 来说似乎是一个很好的案例:

    <%= bar == BAZ ? 'foo' : 'qux' %>
    

    除此之外,一个普通的旧 if/else:

    <%= if bar == BAZ then 'foo'
        else 'qux'
        end
    %>
    

    【讨论】:

      猜你喜欢
      • 2017-05-21
      • 2015-04-27
      • 2023-02-10
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      相关资源
      最近更新 更多