0

これは私を苛立たせています。Railsでこれほど単純なことを行うのがなぜそれほど難しいのかわかりません。

さて、私はインデックスを使ってループし、個々のdivに複数のクラスを割り当てようとしています。一方は変更されていませんが、もう一方はループによって計算されますclass="item item_1" ... class="item item_2"..。これは私がこれまでのコードに対して持っているものです:

<% @variable.each_with_index do |item, index| %>
    <div class=<%="item item_#{index}"%>>
           ....
     </div>
<% end %>

しかし、これは...

<div class="item" item_0="">
....
</div>
<div class="item" item_1="">
....
</div>

これを達成するにはどうすればよいですか?

4

1 に答える 1

2

<div class=<%="item item_#{index}"%>> compiles to <div class=item item_1>, so your browser thinks that the class is item and that item_1 is an attribute.

You just need some quotes around that ERB so it all gets put into the class:

<div class='<%="item item_#{index}"%>'>

should do the trick.

于 2012-07-31T20:40:52.307 に答える