Rails 4 Turbolinks make form submit multiple times

后端 未结 3 836
迷失自我
迷失自我 2020-12-10 08:03

I use some code working nicely on Rails 3 but not on Rails 4, I guess it is caused by Turbolinks but I don\'t know much about it, can\'t dig more deep to solve my problem, h

相关标签:
3条回答
  • 2020-12-10 08:45

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

    0 讨论(0)
  • 2020-12-10 08:51

    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() {
    
    0 讨论(0)
  • 2020-12-10 08:58

    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

    0 讨论(0)
提交回复
热议问题