The dynamic nested form link_to_add is called twice

I am using ryan bates nested_form gem to dynamically add some nested field to the form.

eg.

<%= f.fields_for :phones do |phone_form| %> <%= phone_form.text_field :phone_number %> <% end %> <%= f.link_to_add "Add a phone", :phones %></p> 

Everything works, except TWO empty fields are added every time a link is clicked.

I set a breakpoint on $('form a.add_nested_fields').live('click', function() and I see that it is called twice ...

I am using chrome on mac

+6
source share
4 answers

Look in your headline. You will see that this is twice:

 <script src="/assets/nested_form.js?body=1" type="text/javascript"></script> 

Just remove the second link (maybe in your application application.html.erb) and it will work.

+9
source

you can have downloaded double js files

+1
source

For those who have this problem, using rails 4 / turbolinks and not detecting that nested_form.js are enabled twice, try removing turbolinks from application.js. As soon as I did this and bounced from the server, this problem was resolved.

Credit here: https://github.com/ryanb/nested_form/issues/307

+1
source

check the loading of js files more than one and make sure that you have cleared the browser cache if you are working on projects with multiple projects

-1
source

Source: https://habr.com/ru/post/900802/


All Articles