0

I am able to view 5 asterisks for a rating of 5 for a product, and 4 asterisks for a rating of 4 etc. But what I would like to do is replace the asterisks with an image of a star that I have in my assets/images/ directory, and if a rating is of 4.5 then display half a star. Is there a way of doing this? Below is my current code in application_helper.rb and the view in index.html.erb.

application_helper.rb:

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

index.html.erb:

<div id="star-rating">
    <% if not product.no_of_stars.blank? %>
        <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> 

2 Answers2

4

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
wintermeyer
  • 8,178
  • 8
  • 39
  • 85
  • How would I do this with two different star images? I have star.png that I'm guessing would replace the asterisk and star_half.png that would replace the plus, but I'm not sure how to get them to display? Thanks for answering –  Jan 11 '15 at 15:37
  • I changed the code so that it uses an image to display. – wintermeyer Jan 11 '15 at 15:41
  • That doesn't show the stars, it displays: On the page –  Jan 11 '15 at 15:48
  • Please try it again with the updated code. You need to put the images into app/assets/images/ directory. – wintermeyer Jan 11 '15 at 16:06
  • Did you use the updated code? I updated it multiple times and you need the very last version. – wintermeyer Jan 11 '15 at 16:17
  • Just added it in, it's now not displaying anything, made sure the image names are correct and everything's fine. I don't understand why it's not finding the images –  Jan 11 '15 at 16:24
  • I'd like to help you by chat but for that you'll need 20 reputation. In case you have some loose ends on other questions now would be a good time to accept answers. That'll push you to the 20. – wintermeyer Jan 11 '15 at 16:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68593/discussion-between-benjs-1-and-wintermeyer). –  Jan 11 '15 at 16:41
  • I'm waiting in the chat room. – wintermeyer Jan 11 '15 at 17:05
0

You can try with something like this:

module ApplicationHelper

  def render_stars(value)
    i_value = value.floor
    f_value = value - i_value

    output = '*' * i_value
    output << image_tag('half_star.jpg') if f_value > 0

    output
  end

end

You might have to use .html_safe in the view.

Your html.erb snippet looks wrong, however. It won't render correctly, and it will raise an error if you don't close the first if somewhere down the file. What do you want to achieve?

tompave
  • 11,952
  • 7
  • 37
  • 63
  • That doesn't work, it displays this in the view: Star rating: ***Star half rather than the actual star image –  Jan 11 '15 at 15:31
  • yes. you have to call `html_safe`, as I said. Like this: `render_stars(value).html_safe` – tompave Jan 11 '15 at 16:07