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

前端 未结 2 1338
-上瘾入骨i
-上瘾入骨i 2020-12-11 14:03

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

2条回答
  •  盖世英雄少女心
    2020-12-11 14:59

    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?

提交回复
热议问题