Rails 3 - AJAX REST新表单在单个用户点击时提交

时间:2021-12-07 09:37:27

I have a standard form for the new action of a controller being submitted by AJAX. Every time the user clicks the submit button or presses enter once, the form is submitted twice immediately, creating two identical objects.

我有一个标准表格,用于AJAX提交的控制器的新操作。每次用户单击提交按钮或按Enter键一次,表单将立即提交两次,创建两个相同的对象。

There are no validations on the model and there are instances where that is appropriate.

模型没有验证,有些情况是合适的。

The form view looks like this:

表单视图如下所示:

<%= simple_form_for @contact, remote: true do |f| %>
    <table>
      <tr><td class="cell-right-align">First Name</td><td><%= f.text_field :first_name %></td></tr>
      <tr><td class="cell-right-align">Last Name</td><td><%= f.text_field :last_name %></td></tr>
      <tr><td></td><td><%= f.submit "Create Contact" %></td></tr>
  </table>
<% end %>

The controller action for it:

控制器对它的动作:

def create
  respond_to do |format|
    if @contact.save
      format.js { render 'search_result' }
    else
      format.js { render 'new' } 
    end
  end
end

The logs for the create action show that on the same second there are two POST actions, both identical.

创建操作的日志显示,在同一秒内有两个POST操作,两者都相同。

How can I stop the double POST? I've tried adding :disable_with => 'Saving...' to the submit button and it had no effect.

我怎么能停止双重POST?我尝试添加:disable_with =>'Saving ...'到提交按钮,它没有任何效果。

1 个解决方案

#1


0  

It might be asset pipeline issue. Can you run the app in production mode and verify that the assets are precompiled. I suspect that the javascript used to handle the submit is duplicated. Thus binding two identical events to the form submit.

这可能是资产管道问题。您是否可以在生产模式下运行应用程序并验证资产是否已预编译。我怀疑用于处理提交的javascript是重复的。从而将两个相同的事件绑定到表单提交。

#1


0  

It might be asset pipeline issue. Can you run the app in production mode and verify that the assets are precompiled. I suspect that the javascript used to handle the submit is duplicated. Thus binding two identical events to the form submit.

这可能是资产管道问题。您是否可以在生产模式下运行应用程序并验证资产是否已预编译。我怀疑用于处理提交的javascript是重复的。从而将两个相同的事件绑定到表单提交。