Rails nested_form gem 和 jquery 问题

Rails Issue with nested_form gem and jquery

我有一个嵌套表格并使用 ryan bates 的 nested_form gem,我在该字段中使用了 raty stars,表格给出为

<%= 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" %>

并且 javascript 给出为

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

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

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

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

问题是当我点击添加问题时,之前的星级评分为空,我知道为什么会发生这种情况,因为我再次将评级功能传递给所有星级问题 请告诉我如何添加保留之前星级的问题

试试下面的代码-

$(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
       });
   });
});