2015-12-15 109 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..."}