Skip to content


Subversion checkout URL

You can clone with
Download ZIP


nested_form works wrong with turbolinks #220

wants to merge 1 commit into from

5 participants


Event handlers are executed multiply times



I was having the same problem and this fix worked for me.


I guess #145 could solve turbolinks issues. Could you please test it?


@lest, this commit does fix my problem of multiple events firing

gem 'nested_form', ref: '0607c80', git: ''

Also, i'm seeing nested:fieldAdded firing properly in 0607c80 which wasn't happening in either of these commits

gem 'nested_form', '0.3.1'
gem 'nested_form', '0.3.1', git: ''

Is there a more recent commit that I should be using / testing against?

FWIW, my app also uses simple_form, client_side_validations, backbone, bootstrap (desktop), and jquery_mobile (mobile). I use nested_form on both desktop and mobile.


@ccmcbeck Thanks for testing <3

I'm going to rebase #145 and push it to the master branch.


Thanks to you for working this issue. If you need additional assistance, just let me know.


@ccmcbeck I've pushed jquery code changes from #145 to the master branch of nested_form. Could you please confirm that it's working with turbolinks now?


@lest, when i use this

gem 'nested_form', branch: 'master', git: ''

The edit and delete UJS actions work fine, however only the nested:fieldAdded fires

When I revert back to

gem 'nested_form', ref: '0607c80', git: ''

Both events fire. I checked it in the Safari Web Inspector timeline and with this code:

$(document).on 'nested:fieldAdded', ->
  console.log 'nested:fieldAdded'
$(document).on 'nested:fieldRemoved', ->
  console.log 'nested:fieldRemoved'

@ccmcbeck Weird... I'll try to test it and provide a fix. It'll be very helpful to have a test application reproducing this issue.


It is weird. I will look into it further and isolate the code to see if it's my error.


I am having this issue as well, guess turbolinks can wait. Everytime I use them sooner or later run into one issue or another.


+1, also just realized that turbo-links was causing the link_to_add event to fire multiple times.


@ndbroadbent Any ideas about the best solution for adding event handlers and being compatible with turbolinks?


@lest, I looked into this a little further, and figured out how to make it work. We've got to follow the advice given in the "Evaluating script tags" section of the turbolinks README.

As a rule of thumb when switching to Turbolinks, move all of your javascript tags inside the head and then work backwards, only moving javascript code back to the body if absolutely necessary.

Moving my javascript to the head of the layout solved the problem.


Thanks @ndbroadbent.

Closing as it seems there is no need to make any changes to nested_form.

@lest lest closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 21, 2012
  1. @leoniddinershtein

    fixed Turbolinks delegates click multiply times

    Leonid Dinershtein authored leoniddinershtein committed
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 3 deletions.
  1. +4 −3 vendor/assets/javascripts/jquery_nested_form.js
7 vendor/assets/javascripts/jquery_nested_form.js
@@ -73,9 +73,10 @@ jQuery(function($) {
window.nestedFormEvents = new NestedFormEvents();
- $(document)
- .delegate('form a.add_nested_fields', 'click', nestedFormEvents.addFields)
- .delegate('form a.remove_nested_fields', 'click', nestedFormEvents.removeFields);
+ $(document).off('click', 'form a.add_nested_fields');
+ $(document).on('click', 'form a.add_nested_fields', nestedFormEvents.addFields);
+ $(document).on('click', 'form a.remove_nested_fields', nestedFormEvents.removeFields);
Something went wrong with that request. Please try again.