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

3
  • is that a solution ? i didnt understand Commented Jun 11, 2015 at 8:42
  • @nishantvarshney are you sure this is a valid JS ? Commented Jun 11, 2015 at 8:48
  • @hawk i have edited the JS please go through it Commented Jun 11, 2015 at 8:52

1 Answer 1

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
       });
   });
});
Sign up to request clarification or add additional context in comments.

1 Comment

i have edited your answer you forgot qoutes to put , but this logic worked , thank you so much !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.