0

Hi guys I'm using Star Rating jQuery Plugin Raty to do some review / rating things. For now I'm having a list of data that need to be review, and then the ways to review would be in a boostrap modal box. But however, the rating star are only appearing in the first form of my list, and the star won't show up for the rest of the form. The second form's star will be shown only after the first form's is review and submitted. Example are pictures below.

This is the first form of the list. The stars are showing and able to give rating.

enter image description here

Then following second form in the list, the stars are disappearing from the form, it will be shown only the first form are reviews and submitted.

enter image description here

And below are the final result. But currently I'm able to rating one by one from the top, i wish that i could rating randomly.

enter image description here

Below are my codes for rendering the list in table and modal box.

<% @appointment.approve_applicants.each_with_index do |applicant, index| %>
<tr>
<td><%= index + 1 %></td>
<td><%= link_to applicant.user.fullname, applicant.user %></td>
<td><%= link_to "Reviews", applicant.build_review, "data-toggle" => "modal", "data-target" => "#detail#{applicant.id}" %></td>
<td></td>

<!--Modal-->
<div class="modal fade" id="detail<%= applicant.id %>" role="dialog">
  <div class="modal-dialog">
    <!--Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close close_book_detail" data-dismiss="modal">&times;</button>
        <%= form_for(Review.new) do |f| %>
          <%= render 'shared/error_messages', object: f.object %>
          Rating for user <%= applicant.user.fullname %>
          <div class="form-group row">
            <%= f.label :rating, class: 'col-sm-2 form-control-label' %>
            <div class="col-sm-6" id='star-rating'>
              <%=hidden_field_tag "review[rating]", '', id: "rating_star" %>
            </div>
          </div>
          <%= f.label :comment %>
          <%= f.text_area :comment, placeholder: "Please enter comment here." %>
          <%= f.submit "Save changes", class: "btn btn-primary" %>
        <% end %>
      </div>
    </div>
  </div>
</div>
</tr>
<% end %>

Then below are the javascripts for the jQuery rating stars plugin.

<script>
$('#star-rating').raty({
    path: '/assets',
    target     : '#rating_star',
    targetType : 'score',
    targetKeep : true
});
</script>

<script>
$('.star-rating-show').raty({
    path: '/assets/',
    readOnly: true,
    score: function() {
        return $(this).attr('data-score');
    }
});
</script>

Thank you in advance for helping me!

  • I think it might be caused by your using id #start-rating. Since ids are supposed to be unique on the page, Raty only finds the first one. – EJAg Feb 05 '19 at 09:44
  • Hi @EJ2015 i noticed that too, you have any ways recommend? – Kowa JiaLiang Feb 05 '19 at 11:56
  • Why not use class instead? You can use JQuery ``.children()`` method to find the correct target: ``$(this).children(".rating_start").first()`` – EJAg Feb 05 '19 at 21:15

0 Answers0