Reputation: 222
I'm attempting to add some backend validation to my Ember/Rails app. So, reading all the posts I can find, I have done the following:
In my Rails model, I've added validates_presence_of and validates_uniqueness_of entries.
In the Rails controller, I've got a method that is 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 the Ember route, I have the following:
actions: {
create: function(permission){
var route = this;
permission.save().then(function(){
route.transitionTo('permissions');
});
}
}
When any error is returned from the backend, all I get is a message on the console:
Error: Assertion Failed: Error: The backend rejected the commit because it was
invalid: {name: can't be blank, description: can't be 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 the following in my template:
{{#if isError}}
<p>There was an error saving the 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 is being displayed on the page, only the assertion error in the console log.
I'm not sure what I'm missing, here.
Upvotes: 2
Views: 1457
Reputation: 20125
You need to handle the failure state of the save
-promise to prevent Ember Data from bubbling the failure up to the console.
permission.save().then(function(){
route.transitionTo('permissions');
}, function() {
// Couldn't save, do nothing about it.
});
That should prevent the assertion error from blocking execution and allow Ember Data to work its magic, adding the errors returned by the backend to your model instance.
Upvotes: 3