Dan Brookwell
Dan Brookwell

Reputation: 341

Rails create form submitting multiple times

I have a rails form calling upon the create function in my controller, and I've noticed if i press the submit button multiple times while the page loads, I am able to submit the form multiple times. Is there a way to prevent this from happening? My button is:

<%= button_to edit_production_path(id: current_user.default_working_production_id), class: "btn btn-default navbar-btn", :method => :get do %><span class="glyphicon glyphicon-film"></span>&nbsp; Production Settings<% end %>

And my controller is:

def create
    @production = Production.new(production_params)
    @production.user = current_user
    @production.user_name = current_user.name

    if @production.save
        redirect_to productions_path
    else
        render 'new'
    end
end`

Upvotes: 0

Views: 757

Answers (1)

Austio
Austio

Reputation: 6095

I notice you are using edit_path, which should be a put to the update action, but you are specifying :get as the method. Are you sure this is going to your create action?

In order to prevent double submits you will need to disable with javascript, add this to the button.

data: { disable_with: "Submitting..."}

Upvotes: 3

Related Questions