[英]Ember.js / Rails / Devise - Handling Validation Errors from Server
Hiyo
我對炭黑還很陌生-致力於構建Ember front,並通過Devise進行Rails back認證。 嘗試在客戶端上顯示服務器端錯誤...
我已經閱讀了很多有關此的內容,但似乎沒有任何效果。
登錄頁面位於/ sessions / new
會話新模板(Emblem.js)
Ember.TextField valueBinding="email" placeholder="Email" type="text"
= errors.email
會議新路線
SiloStore.SessionsNewRoute = Ember.Route.extend
model: -> @store.createRecord('session')
setupController: (controller, model) ->
controller.set('content', model)
會話新控制器
SiloStore.SessionsNewController = Ember.ObjectController.extend
needs: ['admin']
actions:{
logIn: ->
self = @
content = @content
@content.save().then(->
self.get('controllers.admin').set('content', content);
self.transitionToRoute 'admin.dashboard'
)
}
會話控制器(Rails)
render json: {
errors: {
email: ["invalid email or password"]
}
}, status: :unprocessable_entity
控制台中Rails Server的JSON錯誤
{"errors":{"email":["invalidemailorpassword"]}}
現在,而不是在模板的Ember.TextField下顯示我的錯誤-我收到了一個看起來像這樣的大紅色丑陋錯誤:
POST http://dev.siloarts.net:3000/api/v1/sessions 422 (Unprocessable Entity)
Error: The backend rejected the commit because it was invalid: {email: invalid email or password}
有任何想法嗎?? 我敢肯定這是愚蠢的事...
哦,哦,這是我的調試信息:
DEBUG: -------------------------------
DEBUG: Ember : 1.4.0-beta.1+canary.011b67b8
DEBUG: Ember Data : 1.0.0-beta.5+canary.d9ce2a53
DEBUG: Handlebars : 1.1.2
DEBUG: jQuery : 1.10.2
DEBUG: -------------------------------
謝謝你的愛
此錯誤也阻止了錯誤結構綁定。 您可以通過以下方式修復/修補:在需要保存時始終調用person.save().catch(->)
。 這將捕獲該錯誤,但不會注意到,但仍然可以在模型上使用becameError:
和becameInvalid
或僅自行實現該功能。 您還可以如下更改模型類:
App.Model = DS.Model.extend
save: (catchErr=true) ->
if catchErr
fail = ->
console.log('Save failed')
retutn @_super().catch(fail)
@_super()
比更改Person對象的基本Callas
App.Person = App.Model.extend
...
然后,當您需要真正的捕獲時,請使用person.save(false).catch(fn)
升級后,我遇到了同樣的錯誤。 降級到Ember 1.2.0應該可以解決您的問題:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.