Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Nested Form link_to_add called twice

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

e.g.

<%= 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 the link is clicked.

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

I am using chrome on a mac

like image 925
OptimisticMonkey Avatar asked Nov 04 '11 19:11

OptimisticMonkey


3 Answers

Look in your header. You'll see that this is in there twice:

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

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

like image 151
Jon J Avatar answered Oct 15 '22 15:10

Jon J


you may have nested form js files being loaded twice

like image 34
Sulabh Jain Avatar answered Oct 15 '22 15:10

Sulabh Jain


For those having this issue using rails 4 / turbolinks and not finding nested_form.js included twice, try removing turbolinks from application.js. Once I did this and bounced the server this issue was resolved.

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

like image 1
kbeal Avatar answered Oct 15 '22 15:10

kbeal