Rails AJAX表单提交模态错误

时间:2017-03-13 10:51:43

标签: ruby-on-rails

我正在尝试添加一个带有表单的bootstrap 4模式,如果表单有任何验证错误,那么在用户点击提交后,这些错误应该显示在模态上。

所以我的表单是使用remote:true发送ajax请求,它击中控制器,我在控制器中使用以下代码:

def create
  if @customer.save
    redirect_to customers_path
  else
    if @customer.errors.any?
      render :new
    end
  end
end

new.js.erb代码如下

$('.modal-content').html("<%= j render 'customers/new' %>");
$('#modal').modal('show');

现在我遇到的问题是错误出现在模态上,但我现在无法在不刷新页面的情况下再次关闭模式。当我尝试关闭模式时,我在控制台 Uncaught Error: Modal is transitioning 中收到以下错误。如果我填写正确的详细信息,我可以提交表格并创建客户,然后关闭模式。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

仅在成功时关闭模态。如果失败了,您就不需要像$('#modal').modal('show');

那样再次打开它

为此,您需要回答JS的成功/失败。如果您在表单上使用remote: true,Redirects将无法正常工作。