Rails创建多次提交表单

时间:2015-12-15 03:02:31

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我有一个rails表单调用我的控制器中的create函数,我注意到如果我在页面加载时多次按下提交按钮,我可以多次提交表单。有没有办法防止这种情况发生?我的按钮是:

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

1 个答案:

答案 0 :(得分:3)

我注意到您正在使用edit_path,这应该是update操作的放置,但您指定:get作为方法。您确定这是create行动吗?

为了防止双重提交,您需要使用javascript禁用,将其添加到按钮。

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