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.
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.
How can I stop the double POST? I’ve tried adding :disable_with => 'Saving...' to the submit button and it had no effect.
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.