2015-06-11 54 views
2

我有一個嵌套的表格,並使用nested_form寶石在裏面我是用RATY明星在現場瑞安貝茨,形式給出Rails的問題與nested_form寶石和jQuery

<%= 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 
       }); 
      }); 

的問題是,當我點擊添加問題上一星評級會空的,我知道爲什麼會這樣,因爲我再次路過RATY功能全明星-問題 請告訴我怎麼能我添加保留前一個明星的問題評級

+0

是一個解決方案嗎?我沒有明白 – user4965201

+0

@nishantvarshney你確定這是一個有效的JS? – hawk

+0

@hawk我已編輯JS請通過它 – user4965201

回答

1

給下面的代碼試戴

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

我編輯了你的答案,你忘了qoutes放,但這個邏輯工作,非常感謝你! – user4965201