catch22
catch22

Reputation: 1693

Raty jQuery plugin not working Rails 5

I'm new to RoR and I'm having issues displaying the Raty plugin on my app. Also, I've noticed that another Js function has stopped working since I've added Raty so I believe there's a conflict between them?

Raty correspondent files have been added: jquery.raty.js // jquery.raty.css // images

All help would be appreciate, thank you!

/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>

Upvotes: 2

Views: 627

Answers (1)

Marta
Marta

Reputation: 36

You didn't copy your config/environments/production.rb in your question, but it probably looks like this: config.assets.compile = false

If you change it to true, it should work: config.assets.compile = true

Upvotes: 2

Related Questions