user2931706
user2931706

Reputation: 195

Rails how to populate stars according to score?

Currently its just populating empty starts:

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

But let's say my score now 3, and it should display following:

enter image description here

Upvotes: 0

Views: 62

Answers (2)

Kyle Decot
Kyle Decot

Reputation: 20815

def display_stars(score)
  (1..5).map do |i| 
    icon(i <= score ? 'star' : 'star-empty')   
  end.join.html_safe
end

Upvotes: 1

Daniel Loureiro
Daniel Loureiro

Reputation: 5363

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: I'm assuming that stars range from 0 to 5

Upvotes: 1

Related Questions