简体   繁体   English

Ruby on Rails图像循环

[英]Ruby on Rails image loop

I'm attempting to create a loop which shows stars as reviews are placed for a product, i have it so that it shows the rating as a number however i wish for it to display an image relating to the rating number, 我正在尝试创建一个循环,显示星星作为产品的评论,我有它,以便它显示评级为数字,但我希望它显示与评级号相关的图像,

i've currently got 我现在有

<%=product.no_of_stars.to_i do image_tag "star-on.png" end %> 

however it just displays the rating figure and not the number, no doubt I've missed something simple. 然而它只是显示评级数字而不是数字,毫无疑问我错过了一些简单的东西。

I've researched other questions and they state that should be enough for what i want, but of course its not working as expected. 我已经研究了其他问题,他们表示应该足够我想要的东西,但当然它没有按预期工作。

Thanks, Ben. 谢谢,本。

Try this. 尝试这个。

<%=product.no_of_stars.to_i.times do image_tag "star-on.png" end %> 

You are missing the times method. 你错过了times方法。 This is what allows you to run the number as a loop over and over again (super simplification). 这使您可以反复运行数字作为循环(超简化)。

The above answer is not quite correct. 上面的答案并不完全正确。 The problem is that Integer#times returns the integer it was called on, so you will still get 5 as the result. 问题是Integer #times返回它调用的整数,因此结果仍然会得到5。 Try 尝试

<% product.no_of_starts.to_i.times do %>
  <%= image_tag "star-on.png" %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM