Get rating value with JQuery Raty on Rails

Viewed 60

have a noob question on jquery raty with rails. I’ve got a star rating system for films. Stars show an average rating for each film on its show page, which I managed to achieve with this code:

<div class='field'> 
<%= form_with(model: [@film, @rating], local: true) do |f| %>
  <div class="star-rating" data-score= <%= @average_rating %> ></div>
  <%= f.submit class: 'btn btn-primary' %>
<% end %>
</div>

<script>
  $('.star-rating').raty({
  path: '/assets/',
  readOnly: false,
  scoreName: 'rating[user_rating]',
  score: function() {
  return $(this).attr('data-score'); 
}
  click: function(score, evt) {
}
});
</script>

But, I want users to be able to click on stars and rating will be automatically recorded to the database. For now, it’s working only with submit button (user should select the number of stars and click 'submit').

As far as I understand, this kind of functionality should be controlled by click: function(score, evt) {}, (that’s why I’ve put it in the code), but struggling to understand what should go there. I would really appreciate any help on where to start building it.

0 Answers
Related