目前它只是填充空白开始:
def icon name
content_tag(:span, nil, class: "glyphicon glyphicon-#{name}")
end
def display_stars score
# here i need to somehow use icon('star')
(1..5).map { |i| icon('star-empty') }.join.html_safe
end
但是我们现在说我的分数是3,它应该显示如下:
答案 0 :(得分:1)
def display_stars score
full = (1..score).map { |i| icon('star') }
empty = (score..4).map { |i| icon('star-empty') }
(full+empty).join.html_safe
end
PS: 我假设stars
的范围是0到5
答案 1 :(得分:1)
def display_stars(score)
(1..5).map do |i|
icon(i <= score ? 'star' : 'star-empty')
end.join.html_safe
end