简体   繁体   English

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

[英]Ember.js / Rails / Devise - Handling Validation Errors from Server

Hiyo Hiyo

I'm pretty new to ember - working on building an Ember front, Rails back authentication with Devise. 我对炭黑还很陌生-致力于构建Ember front,并通过Devise进行Rails back认证。 Trying to show server side errors on the Client... 尝试在客户端上显示服务器端错误...

I've read a bunch of stuff about this but nothing seems to work. 我已经阅读了很多有关此的内容,但似乎没有任何效果。

Login Page is at /sessions/new 登录页面位于/ sessions / new

Sessions New Template (Emblem.js) 会话新模板(Emblem.js)

Ember.TextField valueBinding="email" placeholder="Email" type="text"
= errors.email

Sessions New Route 会议新路线

SiloStore.SessionsNewRoute = Ember.Route.extend

  model: -> @store.createRecord('session')

  setupController: (controller, model) ->
    controller.set('content', model)

Sessions New Controller 会话新控制器

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'
      )
  }

Sessions Controller (Rails) 会话控制器(Rails)

render json: {
  errors: {
    email: ["invalid email or password"]
  }
}, status: :unprocessable_entity

JSON Error from Rails Server in Console 控制台中Rails Server的JSON错误

{"errors":{"email":["invalidemailorpassword"]}}

Now instead of my Error showing under the Ember.TextField in my template - I'm getting a big ugly red error that looks like this: 现在,而不是在模板的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}

Any Ideas?? 有任何想法吗?? I'm sure it's a dumb thing... 我敢肯定这是愚蠢的事...

Oh oh oh and here is my debug info: 哦,哦,这是我的调试信息:

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: ------------------------------- 

THANKYOU IN ADVANCE LOVE HUGH 谢谢你的爱

This error is also preventing errors structure binding. 此错误也阻止了错误结构绑定。 You can fix/patch it this way: call person.save().catch(->) always when you need to save. 您可以通过以下方式修复/修补:在需要保存时始终调用person.save().catch(->) This will catch the error and do noting but one could still use becameError: and becameInvalid on the model or just implement the function on its own. 这将捕获该错误,但不会注意到,但仍然可以在模型上使用becameError:becameInvalid或仅自行实现该功能。 You can also change your model class as follows: 您还可以如下更改模型类:

App.Model =  DS.Model.extend
  save: (catchErr=true) ->
    if catchErr
      fail = ->
        console.log('Save failed')
      retutn @_super().catch(fail)
    @_super()

Than change your base callas for a Person object 比更改Person对象的基本Callas

App.Person =  App.Model.extend 
...   

And then when you need real catch use person.save(false).catch(fn) 然后,当您需要真正的捕获时,请使用person.save(false).catch(fn)

I ran into the same error after upgrading. 升级后,我遇到了同样的错误。 Downgrading to Ember 1.2.0 should solve your problem:) 降级到Ember 1.2.0应该可以解决您的问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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