Created
February 15, 2023 05:24
-
-
Save richlow/f1ea7196567798d8c1753670aa6838b9 to your computer and use it in GitHub Desktop.
Rails star rating helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ################################# | |
| # 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