Ruby 和 Haml Table 中的条件 CSS

Conditional CSS in Ruby and Haml Table

我在 table 中有一个项目列表。有些是真的,有些是假的。有没有一种方法可以使真实元素具有一种背景颜色,而虚假元素具有另一种背景颜色。我正在考虑在应用程序助手中编写一个方法,但我不确定如何最好地编写它。到目前为止我只有

%td.success= person.success

这当前打印出 true 或 false 到 table 但我只想为此添加一些背景颜色?

您可能需要考虑在页面呈现时对一些 JavaScript/jQuery 执行此操作:

$(document).ready(function() {

    $("td").each(function() {

        if ( $(this).html() === true ) {
             $(this).css("background", "blue");
        }
        else {
             $(this).css("background", "red");
        }

    }

}

有了更多关于您呈现的内容 HTML 的上下文,我们可以对其进行调整以获得更具体的内容。

或者为什么不是这样的:

css

td.success { background-color: 'green' }
td.failure { background-color: 'red' }

然后在 haml

%td{class: "#{person.success ? 'success' : 'failure'}"} = person.success

- if person.success
  %td.success = person.success
- else
  %td.failure = person.success