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

'stars' 是一个整数字段。

但它不是渲染图像标签,而是按字面意思显示“星星”数字。

如果我只放了 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_tag以便它不输出stars

于 2010-11-23T21:57:35.300 回答
1

两件事,这不是在 CSS 中使用跨度上的一个类(例如,一星、两星等)来完成的吗?

无论如何,要真正做你想做的事,试试:

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 回答