2016-02-24 76 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

謝謝,請仔細閱讀! –