简体   繁体   English

Rails 3:表单提交两次

[英]Rails 3: Form getting submitted twice

I am developing a Rails application that has forms generated using formtastic. 我正在开发一个Rails应用程序,其中包含使用formtastic生成的表单。 I am developing & testing locally - that is - on localhost:3000 w/ Ruby-1.9.2, Rails-3.0.1, jQuery and AJAX. 我在本地开发和测试 - 就是在localhost:3000 w / Ruby-1.9.2,Rails-3.0.1,jQuery和AJAX。

Below is a sample screen output of the problem I am seeing. 下面是我看到的问题的示例屏幕输出。 My forms are getting submitted twice within 1 second of each other. 我的表单在1秒内相互提交两次。 I can't understand why this is happening. 我不明白为什么会这样。 I see this issue w/ all requests - including GET. 我看到这个问题与所有请求 - 包括GET。

  1. Started POST "/businesses/6/edit_balance" for 127.0.0.1 at 2011-01-07 02:31:47 +0530 Processing by BusinessesController#edit_balance as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"zcWH08sV8kPbAYy7JQX64Cu2e1i/kEB1AB4x5a08CO8=" 在2011-01-07 02:31:47开始POST“/ commerce / 6 / edit_balance”for 127.0.0.1 +0530由BusinessesController处理#edit_balance作为JS参数:{“utf8”=>“✓”,“authenticity_token”= > “zcWH08sV8kPbAYy7JQX64Cu2e1i / kEB1AB4x5a08CO8 =”

  2. Started POST "/businesses/6/edit_balance" for 127.0.0.1 at 2011-01-07 02:31:48 +0530 Processing by BusinessesController#edit_balance as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"zcWH08sV8kPbAYy7JQX64Cu2e1i/kEB1AB4x5a08CO8=" 在2011-01-07 02:31:48开始POST“/ commerce / 6 / edit_balance”for 127.0.0.1 +0530由BusinessesController处理#edit_balance作为JS参数:{“utf8”=>“✓”,“authenticity_token”= > “zcWH08sV8kPbAYy7JQX64Cu2e1i / kEB1AB4x5a08CO8 =”

And so I am wondering whether I am making a basic programming error. 所以我想知道我是否犯了一个基本的编程错误。 If yes, then could you please suggest some solutions that I could try. 如果是的话,那么请您提出一些我可以尝试的解决方案。

I had this same problem right after deploying to Heroku... I pre-compiled my assets, and all of a sudden I was getting double AJAX submissions. 我在部署到Heroku之后就遇到了同样的问题......我预先编译了我的资产,突然间我得到了双重AJAX提交。 I guess I somehow ended up with duplicate javascript files in public/assets. 我想我不知何故最终在公共/资产中出现了重复的javascript文件。

To fix the problem I just deleted my entire public/assets directory. 为了解决这个问题,我刚刚删除了我的整个公共/资产目录。

If you're submitting the form with Javascript, try to set the submit button to be disabled when the form is submitted. 如果您使用Javascript提交表单,请尝试在提交表单时将提交按钮设置为禁用。 With jQuery it would be something like this (not tested): 使用jQuery,它将是这样的(未经测试):

$('form').submit(function(){
  $(this).find(input[type='submit']).attr("disabled", "true");
  ... // submit form via AJAX
  return false;
});

Thanks PolarBlau.. Let me try your suggestion.. 谢谢PolarBlau ..让我试试你的建议..

Apnediving: Below are the bits of code that 1. create the form (form partial) 2. define the dialog that houses the form (dialog partial) and 3. the JS that attaches an action to the form. Apnediving:下面是1.创建表单的代码位(形式部分)2。定义包含表单的对话框(对话框部分)和3.将动作附加到表单的JS。

Form Partial (credits_form) 表格部分(credits_form)

- f.inputs :name => 'Edit Credits' do 
  = f.input :numeric_input_1, :label => 'Amount', :as => :select, :collection => [1000,2000,3000] 
  = f.input :boolean_input_1, :label => 'Add Credits'
  = f.commit_button :label => 'Submit' 

Dialog Partial 对话部分

#edit-credits-dialog #编辑学分,对话

- @user_input = UserInput.new - @user_input = UserInput.new

 = semantic_form_for @user_input, :remote => true do |f|     
 = render :partial => 'businesses/credit_form', :locals => {:f => f}

JS Code JS代码

$.getJSON('/businesses/' + id + '/load_credits', function(data) { 
    var form = $('#edit-credits-dialog form') ; 

    form.attr('action', '/businesses/' + id + '/edit_balance') ; <-- seems to be happening twice
    $('#edit-credits-dialog').dialog('open') ; <--- happens once 
}) ; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM