从Ruby / Rails中的变量中分配CSS类

时间:2012-12-10 04:34:16

标签: css ruby-on-rails

我正在尝试根据列表中的索引来分配元素的CSS。

在这里,我将'q-light'或'q-dark'存储在变量'styling1'中,并且它完美地运行了。

    <% answers.each_with_index do |answer, i| %>
    <% styling1 = i % 2 == 0 ? 'q-light' : 'q-dark' %>

    <div class="<%= styling %>">

但是,当我尝试将索引存储在变量'styling2'中,并尝试将其添加到另一个CSS类时,所以说每个元素的CSS将是'otherclass 0','otherclass 1,'和所以,它给我一个语法错误,说我有一个意外的'&gt;'。

    <% answers.each_with_index do |answer, i| %>
    <% styling2 = i %>

    <%= f.radio_button :answer_id, answer.id%>
    <%= f.label :answer_id, answer.answer, :class => "<%= otherclass styling =>" %> 

我不确定错误的来源。我知道CSS类不应该以数字开头,但我用unicode转义字符命名它们,而不是0在我的css文件中它是\ 30,并且该样式在其他地方工作,所以我不认为这是数字。当我分配像“:class =&gt;”这样的类时,我不能调用变量吗?

1 个答案:

答案 0 :(得分:2)

使用"#{otherclass styling}"代替<%= otherclass styling %>