2015-12-15 106 views
0

我有一個Rails窗體調用我的控制器中的創建函數,並且我注意到如果在頁面加載時多次按下提交按鈕,我可以多次提交表單。有沒有辦法來防止這種情況發生?我的按鈕是:Rails創建表單提交多次

<%= 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 %>

我的控制器:

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` 

回答

3

我注意到你正在使用edit_path,這應該是一個付諸update行動,但要指定:get的方法。你確定這是你的create行動嗎?

爲了防止雙重提交,您需要禁用javascript,將其添加到按鈕。

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