繁体   English   中英

Ember.js / Rails / Devise-处理来自服务器的验证错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM