Rails如何根据得分填充星星?

时间:2015-01-25 23:55:14

标签: ruby-on-rails twitter-bootstrap

目前它只是填充空白开始:

enter image description here

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,它应该显示如下:

enter image description here

2 个答案:

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