jQuery插件不工作Rails 5

Raty jQuery plugin not working Rails 5

本文关键字:Rails 工作 插件 jQuery      更新时间:2023-09-26

我是RoR的新手,我在应用程序上显示Raty插件时遇到了问题。此外,我注意到另一个Js函数已经停止工作,因为我已经添加了Raty,所以我相信他们之间有冲突?

已经添加了Raty对应文件:jquery.raty.js//jquery.raty.css//images

所有的帮助将是感激的,谢谢!

/模型/product.rb

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

/资产/javascript/application.js

$(document).on('turbolinks:load', function () {
  $(".alert").delay(1500).fadeOut("slow");
});
$(document).on('turbolinks:load', function () {
  $(".notice").delay(1500).fadeOut("slow");
});

/资产/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');
      }
    });
});

/视图/产品/_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 %>

/视图/产品/_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>

/视图/产品/show.html.erb

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

你没有在你的问题中复制你的config/environments/production.rb,但它可能看起来像这样:config.assets.compile = false

如果你把它改为true,它应该可以工作:config.assets.compile = true