Rails 4 Turbolinks make form submit multiple times

ぃ、小莉子 提交于 2019-11-28 10:22:43
William Herry

Solved this by moving = javascript_include_tag "application", "data-turbolinks-track" => true from body to head, thanks all your help

You can leave it in the body, you just need to add to your script tag:

"data-turbolinks-eval" => false

In general, with turbolinks, it's best to make sure your code is "idempotent", so if it runs more than once, bindings won't get setup more than once.

The best way to do this is instead of $('blah').bind(), call unbind first:

 $('blah').unbind('click').bind('click', function() {

One possible reason you could be running into issues is if you are including the js on every page. It's my understanding it will append the js to the head and if you have included it on multiple pages you could find yourself binding the ajax multiple times. That being said it's not apparent how you are including the js from what I saw. You could possibly solve this by only including the js file in your application.js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!