在Rails 3 app上呈现的星星

时间:2013-11-19 18:03:16

标签: image view ruby-on-rails-3.2 src

我有一个Rails 3应用程序,用户可以在评论中添加评分,评分为1到5之间的整数。 继Ryan Bates railscast之后,我想使用星系统显示该评级,因此我将这些方法添加到我的ApplicationController中:

def render_stars(rating)
  content_tag :div, star_images(rating), :class => 'stars'
end

def star_images(rating)
  (0...5).map do |position|
    star_image(((rating-position)*2).round)
  end.join
end

def star_image(value)
  image_tag "/images/star-#{star_type(value)}.png", :size => '15x15'
end

def star_type(value)
  if value <= 0
    'off'
  elsif value == 1
    'half'
  else
    'on'
  end
end

在我的评论#index视图中,当我添加行:

<%= render_stars(review.rating) %>

这些方法运行正常,但我没有在我的视图中获取星星的图像,而是获得了html:

<img alt="Star-on" height="15" src="/images/star-on.png" width="15" /><img alt="Star-on" height="15" src="/images/star-on.png" width="15" /><img alt="Star-on" height="15" src="/images/star-on.png" width="15" /><img alt="Star-on" height="15" src="/images/star-on.png" width="15" /><img alt="Star-off" height="15" src="/images/star-off.png" width="15" />

任何想法为什么它没有显示图像而我得到了这些线?

由于

1 个答案:

答案 0 :(得分:0)

请尝试html_safe,如下所示:

star_images(rating).html_safe
相关问题