ember.js - Ember and Rails, Handling backend errors -
i'm attempting add backend validation ember/rails app. so, reading posts can find, have done following:
in rails model, i've added validates_presence_of , validates_uniqueness_of entries.
in rails controller, i've got method called after attempting save:
def validate(permission) if permission.valid? render json: permission else render json: {errors: permission.errors}, status: :unprocessable_entity end end
in ember route, have following:
actions: { create: function(permission){ var route = this; permission.save().then(function(){ route.transitionto('permissions'); }); } }
when error returned backend, message on console:
error: assertion failed: error: backend rejected commit because invalid: {name: can't blank, description: can't blank} "invaliderror@http://127.0.0.1/assets/ember-data.js?body=1:3512 activemodeladapter<.ajaxerror@http://127.0.0.1/assets/ember-data.js?body=1:235 superwrapper@http://127.0.0.1/assets/ember.js?body=1:1295 restadapter<.ajax/</hash.error@http://127.0.0.1/assets/ember-data.js?body=1:1530 jquery.callbacks/fire@http://127.0.0.1/assets/jquery.js?body=1:3100 jquery.callbacks/self.firewith@http://127.0.0.1/assets/jquery.js?body=1:3212 done@http://127.0.0.1/assets/jquery.js?body=1:9313 .send/callback@http://127.0.0.1/assets/jquery.js?body=1:9721 "
i have following in template:
{{#if iserror}} <p>there error saving record</p> {{/if}} {{#each error in errors.name}} <p>{{error.message}}</p> {{/each}} {{#each error in errors.description}} <p>{{error.message}}</p> {{/each}}
however, nothing being displayed on page, assertion error in console log.
i'm not sure i'm missing, here.
you need handle failure state of save
-promise prevent ember data bubbling failure console.
permission.save().then(function(){ route.transitionto('permissions'); }, function() { // couldn't save, nothing it. });
that should prevent assertion error blocking execution , allow ember data work magic, adding errors returned backend model instance.
Comments
Post a Comment