Skip to content

Instantly share code, notes, and snippets.

@richlow
Created February 15, 2023 05:24
Show Gist options
  • Select an option

  • Save richlow/f1ea7196567798d8c1753670aa6838b9 to your computer and use it in GitHub Desktop.

Select an option

Save richlow/f1ea7196567798d8c1753670aa6838b9 to your computer and use it in GitHub Desktop.
Rails star rating helper
#################################
# Pre-req: install font awesome (fonts)
#################################
#star_rating_helper.rb
module StarRatingHelper
def star_rating(rating)
full_star = "fas fa-star"
empty_star = "far fa-star"
stars = []
for i in 1..5
if i <= rating
stars << full_star
else
stars << empty_star
end
end
stars.map do |star|
content_tag(:i, "", class: star)
end.join("\n").html_safe
end
end
#################################
You call this in the view file
<%= star_rating(space.rank) %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment