我正在觀看RailsCast(#101),其中的作者使用一些方法爲博客製作星級評分系統。在方法star_image
s中,他在塊的末尾調用加入。我認爲這將數組轉換爲字符串,但爲什麼需要這樣做?Ruby:在塊之後調用'join'
查看
<%= render_stars(product.rating) %>
Application_helper.rb
def render_stars
content_tag :div, star_images, :class => 'stars'
end
私人
def star_images
(0...5).map do |position|
star_image(((@rating-position)*2).round)
end.join
end
def star_image(value)
image_tag "/images/#{star_type(value)}_star.gif", :size => '15x15'
end
def star_type(value)
if value <= 0
'empty'
elsif value == 1
'half'
else
'full'
end
end