2

I have a nested form and using nested_form gem by ryan bates in which i am using raty stars in the field, the form is given as

<%= f.fields_for :round_questions do |question| %>
     <%= question.label :question %>
     <%= question.text_field :question %>

     <div class="star-questions" > </div>
     <%= question.text_field :answer %>

<% end %>

<%= f.link_to_add "Add a Question", :round_questions,
  :class=> 'btn waves-effect waves-light btn-medium custom_btn_gray',:id => "add-fields" %>

and the javascript is given as

$(document).ready(function() {
    $('.star-questions').raty({

        targetType : 'score',
        targetKeep : true
});

            $(document).on('click', '#add-fields', function(){

                $('.star-questions').raty({
                    targetType : 'score',
                    targetKeep : true
                });
            });

the issue is when i click on add a question the previous star rating are going empty , i know why this is happening because i am passing raty function again to all star-question please tell me how can i add question preserving the previous star rating

user4965201
  • 973
  • 1
  • 11
  • 25

1 Answers1

1

Give the following code a try-

$(function(){
  $('.star-questions').raty({
        targetType : 'score',
        targetKeep : true
      });

     $(document).on('nested:fieldAdded:round_questions', function(event){
        event.field.find('.star-questions').raty({
          targetType : 'score',
          targetKeep : true
       });
   });
});
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Darpa
  • 396
  • 1
  • 8