简体   繁体   中英

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

Hiyo

I'm pretty new to ember - working on building an Ember front, Rails back authentication with Devise. 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 Template (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)

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

JSON Error from Rails Server in Console

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

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

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

And then when you need real catch use person.save(false).catch(fn)

I ran into the same error after upgrading. Downgrading to Ember 1.2.0 should solve your problem:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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