rails / jquery-ujs

Ruby on Rails unobtrusive scripting adapter for jQuery
MIT License
2.61k stars 508 forks source link

Form rendered by js.erb does not submit by ajax #504

Closed wordjelly closed 6 years ago

wordjelly commented 6 years ago

Hi,

  1. My app makes an ajax call to a controller.
  2. The controller responds with a js.erb view that has a form inside it.
  3. That form has a (remote: true) tag added to it.
  4. When i try to submit that form, it is submitted as html.

I have been googling for several hours, and although several people seem to have had issues with this, I cannot find a definitive way around it.

I tried disabling turbolinks, but it does not make any difference.

Versions: Rails: (4.2.10) Jquery - rails : 4.2.2

Thanks, Bhargav

wordjelly commented 6 years ago

Hi,

I was able to solve this problem. I am posting my specific scenario here, in case anyone else needs help. The issue was that I was rendering a form in the js.erb that had the same id as a form that was already on the earlier page. Even though I was replacing the older form by writing out entirely new content to the parent div, it seems the jquery binding does not occur if an element with the same id was previously present on the page. I changed the ids, so that unique ids are generated, and the form began submitting as ajax.

HTH.