Ruby on Rails Display Half a Star for a Decimal Rating, E.G. 4.5

Ruby on Rails display half a star for a decimal rating, e.g. 4.5

Let's assume you want to use star.gif as a star image and half-star.gif as the 1/2 star image:

module ApplicationHelper
def render_stars(value)
output = ''
if (1..5).include?(value.floor)
value.floor.times { output += image_tag('star.gif')}
end
if value == (value.floor + 0.5) && value.to_i != 5
output += image_tag('half-star.gif')
end
output.html_safe
end
end

Rails4: How to display 5 star images

I've reworked your code a little to try and handle all the cases. See comments.

def render_stars(value)
output = ''

# Saved to variable for efficiency
floored = value.floor

# Adding full stars: If value is 2.5 then floored
# will be 2 (two full stars)
floored.times { output << image_tag('star-on.png') }

# Getting the decimal part of the value by subtracting the
# floored value, by the value. So if value is 2.7, then will be
# 0.7 (Changed to >= to account for ability to use numbers like
# 2.7 and will display a half star. If you would like to only
# handle 0.5 then change back to ==)
if (value - floored) >= 0.5
output << image_tag('star-half.png')
end

# Completes sequence of 5 stars. Ex: if value is 2.7 then 5 - 2.7
# is 2.3 The output will already have 2 full stars and 1 half star
# so by flooring 2.3 we will get 2, which completes the total of 5
#stars
(5 - value).round.times { output << image_tag('star-off.png') }
output.html_safe
end

Ruby on rails display star images for the rating system

You can do this with a helper. In case you need the stars in different models you can add a helper in the file app/helpers/application_helper.rb (I use '*' as a representation for a star):

module ApplicationHelper

def render_stars(value)
output = ''
if (1..5).include?(value.to_i)
value.to_i.times { output += '*'}
end
output
end

end

Your refactored view would look like this:

<div id="star-rating">
<% if product.no_of_stars.blank? %>
<%= link_to 'Please add a Review',{ controller: 'reviews', action: 'new', id: product.id } %>
<% else %>
Star rating: <%= render_stars(product.no_of_stars) %>
<% end %>
</div>

Displaying Star Image According Integer In Database

This should really go into a view helper.

def show_stars(review)
image_tag "#{review.rating}star.png"
end

Then in your view:

<%= show_stars @review %>

Proper way to render fa_icons in helpers in Rails

There are two errors you're making that are super-small.

First, is in the parameters you're giving to content tag. Take a look at the documentation. In the first example...

content_tag(:p, "Hello world!")
# => <p>Hello world!</p>

The string is what goes between the tags. However, Font Awesome icons need to be set as the class of the tag, with no content between the <i></i>.

Which means you need to pass content_tag an empty string and a hash...

<%= content_tag(:i, "", class: "fa-icon star") %>
=> "<i class="fa_icon star"></i>"

I know you're doing other things to multiply stars and whatnot. I think you'll be able to take it from here...

Round a ruby float up or down to the nearest 0.05

Check this link out, I think it's what you need.
Ruby rounding

class Float
def round_to(x)
(self * 10**x).round.to_f / 10**x
end

def ceil_to(x)
(self * 10**x).ceil.to_f / 10**x
end

def floor_to(x)
(self * 10**x).floor.to_f / 10**x
end
end


Related Topics



Leave a reply



Submit