Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 Raty的平均评级为';我的循环中没有工作_Ruby On Rails 4_Foreach_Raty - Fatal编程技术网

Ruby on rails 4 Raty的平均评级为';我的循环中没有工作

Ruby on rails 4 Raty的平均评级为';我的循环中没有工作,ruby-on-rails-4,foreach,raty,Ruby On Rails 4,Foreach,Raty,我在用Raty做评论。我想在循环中显示每个产品的平均分数 如果我只使用,那么每个产品都将显示正确的平均数量/评级。如果我通过id将字段连接到一个嵌入式脚本以获取星星,那么第一个对象只有5个空星星,其他对象只有nill。 同样的脚本在我的产品视图中也可以使用 这是我的视图代码,没有脚本: <div class="row"> <br/> <%= will_paginate @products %> <% @products.each do |product|

我在用Raty做评论。我想在循环中显示每个产品的平均分数

如果我只使用
,那么每个产品都将显示正确的平均数量/评级。如果我通过
id
将字段连接到一个嵌入式脚本以获取星星,那么第一个对象只有5个空星星,其他对象只有nill。 同样的脚本在我的产品视图中也可以使用

这是我的视图代码,没有
脚本

<div class="row">
<br/>
<%= will_paginate @products %>
<% @products.each do |product| %>
    <%= link_to product do %>   
    <div class="col s6 m4 l2">
            <div class="card" style="height: 50px">     
                <div class="row">
                    <div class="col s4 m4 l4">                              
                        <% if !product.external_image.nil? %>
                        <div class="card-image waves-effect waves-block waves-light valign-wrapper">
                            <%= image_tag product.external_image, class: "valign" %>
                        </div>  
                        <% else %>                              
                        <div class="card-image waves-effect waves-block waves-light valign-wrapper">
                            <%= image_tag product.image, class: "responsive-img" %>
                        </div>
                        <% end %>                                       
                    </div>
                    <div class="col s8 m8 l8">
                        <div class="grey-text text-darken-4 truncate valign" style="height: 25px">
                            <%= product.name %>
                        </div>
                        <div class="divider"></div>
                        <span style="height: 25px">
                        <%= product.average_rating %>
                        </span>             
                    </div>
                </div>
            </div>
        </div>  
    <% end %>
<% end %>
<%= will_paginate @products %>
<div class="row">
<br/>
<%= will_paginate @products %>
<% @products.each do |product| %>
    <%= link_to product do %>   
    <div class="col s6 m4 l2">
            <div class="card" style="height: 50px">     
                <div class="row">
                    <div class="col s4 m4 l4">                              
                        <% if !product.external_image.nil? %>
                        <div class="card-image waves-effect waves-block waves-light valign-wrapper">
                            <%= image_tag product.external_image, class: "valign" %>
                        </div>  
                        <% else %>                              
                        <div class="card-image waves-effect waves-block waves-light valign-wrapper">
                            <%= image_tag product.image, class: "responsive-img" %>
                        </div>
                        <% end %>                                       
                    </div>
                    <div class="col s8 m8 l8">
                        <div class="grey-text text-darken-4 truncate valign" style="height: 25px">
                            <%= product.name %>
                        </div>
                        <div class="divider"></div>
                        <span id="average_rating" style="height: 25px">
                        </span>             
                    </div>
                </div>
            </div>
        </div>
        <script>
            $('#average_rating').raty({
                path: '/assets',
                readOnly: true,
                score: <%= product.average_rating %>
            });
        </script>   
    <% end %>
<% end %>
<%= will_paginate @products %>

有没有想过我在这里做错了什么以及如何修复它

解决方案: @user2856118为我的回答提供了依据。我只需要稍微调整一下(与下面的调整答案相比)。这是我的工作代码:

跨度:


脚本:

        <script>
            $('#average_rating_<%= product.id %>').raty({
                path: '/assets',
                readOnly: true,
                score: <%= product.average_rating %>
            });
        </script>   

$('平均评级').raty({
路径:'/assets',
只读:对,
分数:
});
更改为
然后在你的脚本中

$('#average_rating_<%=product.id%>').raty({
            path: '/assets',
            readOnly: true,
            score: <%= product.average_rating %>
        });
$('average'u rating').raty({
路径:'/assets',
只读:对,
分数:
});

如果一个产品没有评级,也就是说如果评级为零,那么平均评级能处理这个问题吗?你能做一些像product.reject{p{p.rating.nil?}这样的事情吗?average_rating我真的不知道你的关联,所以我只是在这里猜测。也许product.average_rating重新运行一个对象,raty不知道如何处理它。若你们在控制台上进行平均_评级,它会吐出什么?你需要这样做吗?@user2856118该脚本在Product show视图中工作,并且还处理
nill
,这似乎不是问题。我的另一个猜测是将#average_rating id更改为一个类。您将只有一个同名的id。Jquery知道这一点,并且只对循环中的第一个有效。或者让他们成为uniq。使用product.id以便脚本可以找到正确的span@user2856118在将id更改为类的情况下:循环显示
数组中我的第一个产品的平均评级,它不正确(
nill
)。不应该显示正确的,它只会显示第一个id为
id
的对象?我之前已经尝试过这个方法(实际上这似乎是最好的解决方案),但它不会返回任何结果(至少没有星星,我假设
nill
应该返回空星星)是的,nil应该返回空星星。祝你好运解决这个问题。如果这是最好的解决方案,请标记为答案。如果你以后找到更好的解决方案,也可以在这里发布,以便保存以备日后参考。空星还是无星?我不确定,我不知道raty是如何工作的。但我认为它应该返回空星。也许这是一个bug,如果它没有评级,它根本就不会显示星星。也许你必须给它一个初始评分为0。我添加了一些额外的代码,Raty在
nil
时返回空星。所以它应该会起作用,不是吗?
$('#average_rating_<%=product.id%>').raty({
            path: '/assets',
            readOnly: true,
            score: <%= product.average_rating %>
        });