【问题标题】:Rails - using CSS on some conditionRails - 在某些情况下使用 CSS
【发布时间】:2013-05-21 09:29:36
【问题描述】:

我需要制作一个有视图的应用程序。在那个视图中,我需要检查一个条件,如果它是真的,那么需要适当地为表格行着色。最简单的方法就是在视图中使用不同的标题。但是,如果我想将所有样式信息保存在 CSS 中,该怎么做呢?

【问题讨论】:

    标签: css ruby-on-rails conditional-statements


    【解决方案1】:

    如果它只是你想要着色的一行,那么你可以在视图中做,不需要乱用标题:

    <tr class="<%= "blue" if some_condition %>">
      <td>Your text</td>
    </tr>
    

    或者:

    <% if some_condition %>
      <tr class="blue">
    <% else %>
      <tr class="red">
    <% end %>
      <td>Your text</td>
    </tr>
    

    【讨论】:

    • 谢谢!我只是忘记上课了:P
    【解决方案2】:

    application_helper.rb

    def my_color_for(condition)
      if condition
        'white'
      else
        'blue'
      end
    end
    

    view.haml

    - @array.each do |a|
      %tr{class: my_color_for(a.value)}
    

    【讨论】:

      猜你喜欢
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 2012-02-03
      相关资源
      最近更新 更多