Raty jQuery插件不能正常使用Rails 5

时间:2016-09-06 10:02:18

标签: javascript jquery ruby-on-rails show raty

我是RoR的新手,我在我的应用上显示Raty插件时出现问题。 另外,我注意到另外一个Js功能已经停止工作,因为我已经添加了Raty,所以我相信它们之间存在冲突?

添加了Raty通讯员文件:jquery.raty.js // jquery.raty.css // images

所有的帮助将不胜感激,谢谢!

/models/product.rb

def average_rating
comments.average(:rating).to_f
end

/assets/javascript/application.js

$(document).on('turbolinks:load', function () {

  $(".alert").delay(1500).fadeOut("slow");

});

$(document).on('turbolinks:load', function () {

  $(".notice").delay(1500).fadeOut("slow");

});

/assets/javascript/site.js

$(document).on('turbolinks:load', function(){
    $('.rating').raty( { path: '/assets', scoreName: 'comment[rating]' });
    $('.rated').raty({ path: '/assets',
      readOnly: true,
      score: function() {
        return $(this).attr('data-score');
      }
    });
});

/views/products/_new_comment.erb

<% if signed_in? %>
  <h2>Add a review:</h2>
  <%= form_for([@product, @product.comments.build]) do |f| %>
    <p>
      <%= f.label :body, "Comments" %><br>
      <%= f.text_area :body %>
    </p>
    <p>
      <%= f.label :rating, "Rating" %><br>
      <div class="rating"></div>
    </p>  
    <br>
    <p>
      <%= f.submit "Submit" %>
    </p>
  <% end %>
<% end %>

/views/products/_comments.erb

<div class="product-reviews">
    <% @comments.each do |comment| %>
        <div class="row">
            <hr>
            <p><%= comment.user.first_name %> <small><em><%= "#{time_ago_in_words(comment.created_at)} ago" %></em></small></p>
            <p><div class="rated" data-score="<%= comment.rating %>"></div></p>
            <p><%= comment.body %></p>
        </div>
    <% end %>
</div>

/views/products/show.html.erb

<div class="col-md-12">
    <%= render partial:'new_comment' %>
</div>

<div class="col-md-12">
    <%= render partial:'comments' %> 
</div>

1 个答案:

答案 0 :(得分:2)

您没有在问题中复制config/environments/production.rb,但它可能如下所示: config.assets.compile = false

如果将其更改为true,则应该有效: config.assets.compile = true