【问题标题】:Rails cycle helper with a few exceptionsRails 循环助手,有一些例外
【发布时间】:2011-03-03 20:41:09
【问题描述】:

我以标准方式使用 Rails cycle() 辅助方法与表格行来使交替行具有不同的背景颜色。但是,我希望偶尔有一两行(符合某些标准)是不同的第三种颜色,而不会中断循环。

换句话说,我想要这样的行:

white
black
red
black
white
black
white

代替:

white
black
red
white
black
white

最好的方法是什么?

【问题讨论】:

    标签: ruby-on-rails helper cycle alternating


    【解决方案1】:

    必须将它存储在一个临时变量中并调用 cycle() 以确保它是最新的。

    <%
    class = cycle('white', 'black', :name => 'colors')
    class = 'red' if should_be_highlighted
    %>
    <tr class="<%= class %>">
    

    你可以用你自己的助手很好地把它包装起来。

    【讨论】:

    • NB 在大多数语言中使用class 作为变量名的坏习惯
    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多