0

モデル属性に基づいて星評価を表示するヘルパーを作成しようとしています。

私は次のものを持っています:

  def show_profile_stars(profile)
    content_tag :span, :class => 'stars' do
      profile.stars.times do
        image_tag("stars.gif", :size => "30x30", :class => "gold")
      end
    end
  end

「星」は整数フィールドです。

ただし、画像タグをレンダリングするのではなく、「星」の数を文字どおりに表示するだけです。

反復ブロックなしで image_tag だけを配置すると、画像が表示されますが、問題は反復にあります。

ブロックを受け取るメソッドについて何かが欠けていると思います (私はまだ RoR の初心者です)。

何か助けはありますか?

ありがとう!

4

2 に答える 2

2

concatヘルパーを使用します。

def show_profile_stars(profile)
  content_tag :span, :class => 'stars' do
    profile.stars.times do
      concat(image_tag("stars.gif", :size => "30x30", :class => "gold"))
    end

    nil
  end
end

出力されないようnilに、最後に戻る必要もあります。content_tagstars

于 2010-11-23T21:57:35.300 に答える
1

2 つのことですが、これは CSS でスパンのクラス (1 つ星、2 つ星など) を使用して行われませんか?

とにかく、実際にやりたいことを実行するには、次のことを試してください。

stars = []
profile.stars.times { stars << image_tag("stars.gif", :size => "30x30", :class => "gold") }
content_tag :span, stars.join("\n").html_safe, :class => "stars"
于 2010-11-23T21:10:49.333 に答える