I am able to view 5 stars for a rating of 5 for a article, and 4 stars for a rating of 4 etc. I reference Ruby on Rails display half a star for a decimal rating, e.g. 4.5.
What I'd like to do is to display "star-off.png" if a rating is not 5 stars. For example, if a rating is 3, display 3 "star-on.png" and 2 "star-off.png".
Although I know some gems such as ratyrate, I'd like to know how to describe without using gems.
\app\helpers\application_helper.rb
...
def render_stars(value)
output = ''
if (1..5).include?(value.floor)
value.floor.times { output += image_tag('star-on.png')}
end
if value == (value.floor + 0.5) && value.to_i != 5
output += image_tag('star-half.png')
end
output.html_safe
end
...
\app\views\articles\ _article.html.erb
...
<% if article.rating.blank? %>
<% else %>
<%= render_stars(article.rating) %>
<% end %>
...