如何根据 if else 条件使用 activeadmin 制作彩色行

How to make a row in color with activeadmin based on if else condition

我是 Rails 上 Ruby 的新手。我正在为我的应用程序使用 Active Admin。如果条件为真而其他一些颜色为假,我想为一行着色。

tr.odd, tr.even {
    &.test {
        td.col {
            background-color: red;
        }
    }
}

我想将此条件应用于测试 class。有什么办法可以完成这个任务吗?

我建议使用两个不同的 CSS 类 然后在您的 ERB 视图中应用不同的 类 例如:

<tr class="<%= condition ? 'test' : 'default' %>">
  <td>1</td>
  <td>2</td>
  <td>3</td>
</tr>

理想情况下,您会将其移至这样的助手

# application_helper.rb
def css_class_for(condition)
  if condition
    'test-css-class'
  else
    'other-css-class'
  end
end

# view
<tr class="<%= css_class_for(true) %>">
  <td>1</td>
  <td>2</td>
  <td>3</td>
</tr