开发者

Put An Image Inside A Rails Helper

Hullo,

I'm trying to make a website in ruby on rails and I'm building a helper to display a rating out of 5 as a series of stars. So far I have got:

def stars(score)
html = ""
if score >= 1
  image_tag "star.png", :alt => 'one'
else
  html << ""
end
if score >= 2
  html << (image_tag "star.png", :alt => 'one')
else
  html << ""
end
if score >= 3
  html << (image_tag "star.png", :alt => 'one')
else
  html << ""
end
if score >= 4
  html << (image_tag "star.png", :alt => 'one')
else
  html << ""
end
if score >= 5
  html << (image_tag "star.png", :alt => 'one')
else
  html << ""
end
end
开发者_C百科

but that does not seem a very good technique and it is writing out the html to the screen instead of showing an image.

Anyone have any ideas what I can do?

Thanks, Haziba


def stars(score)
  (image_tag("star.png", :alt => 'one') * score).html_safe
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜