1

object.each do |x|.activeクラスを適用して、コマンドを介して出力される最初の要素のスタイルを設定しようとしています。私はそれを理解することはできません-どうすればいいですか?

4

3 に答える 3

4

を使用しeach_with_index()ます。わかりやすくするために、ERB以外の例を以下に示します。

['hello', 'world'].each_with_index do |item, index|
  if index == 0
    puts "This is the first item"
  end

  puts item
end

プリントアウト:

This is the first item
hello
world
于 2012-11-09T11:50:27.847 に答える
1

それは非常に明白なようです:

objects.first.css_options += ' .active'

次に、通常の方法ですべてのオブジェクトを反復処理します。

バリエーションが異なる場合があります。たとえば、最後の要素にもcssオプションを適用する必要があります。

objects.zip(['active','','',...]).each do |obj,klass|
  obj.css_option += klass
  ...
end
于 2012-11-09T12:25:55.177 に答える
0
[obj1, obj2].each_with_index do |item, index|
  item.css_option += ' .active' if index == 0
end
于 2012-11-09T13:08:06.900 に答える