2016-02-24 80 views
1

我有AJAX创建新项目并在我看来更新它们。AJAX验证编辑/更新

我的模型验证在创建新模型时不会保存或提交。

但是,当我更新现有的字段时,它只会停止保存它,但会呈现带有“新”数据的ajax更新表单,从而给出它正在保存的印象。

当我刷新后,我看到变化得到了回滚。但这只是我想要的行为的一半。

我需要提交按钮被禁用。我怎样才能做到这一点?

我的表格样式是这样的:

<%= f.text_field :name, required: true, class: "mdl-textfield__input" %> 

和我在模型验证是这样的:

validates_presence_of :name 

我edit.js.erb

$("#education_<%[email protected]_education.id%>").replaceWith("<%= j render('new_employee_education_form')%>"); 

我的更新。 js.erb

$("#edit_employeeeducation_<%= @employee_education.id %>").replaceWith('<%= j render partial: "employeeeducation", locals: {employee_education: @employee_education} %>'); 

enter image description here

+1

基本上,你没有处理错误,如果有错误,你应该再次渲染表单,不显示部分输出数据。 –

回答

1

也许你可以添加一些使用jQuery插件例如formvalidator.net的前端表单验证。如果数据不正确,您可以使用它来防止首先提交表单。

或者在您的update.js.erb中,您应该向用户发送一些反馈,说明数据出现问题的原因。

+0

谢谢,请仔细阅读! –