abuiles / ember-cli-101-errata

18 stars 4 forks source link

Page 61: Attempting to save in console results in bad URL/404, not invalid model error #171

Closed jbinto closed 9 years ago

jbinto commented 9 years ago

On page 61, when I run:

$E.get('articles').createRecord({description: 'foo'}).save()

I don't get the expected error from the book: Error: The backend rejected the commit because it was invalid: {state: can't be \ blank,is not included in the list}

Instead, I get the following:

 POST http://localhost:4200/friends/721/articles/null 404 (Not Found)
ember.debug.js:28602 Error: Not Found
    at ember$data$lib$system$adapter$$Adapter.extend.ajaxError (rest_adapter.js:820)
    at superFunction [as _super] (ember.debug.js:14376)
    at ember$data$lib$adapters$rest_adapter$$default.extend.ajaxError (active_model_adapter.js:141)
    at apply (ember.debug.js:18630)
    at superWrapper [as ajaxError] (ember.debug.js:18195)
    at ember$data$lib$system$adapter$$Adapter.extend.ajax.Ember.RSVP.Promise.hash.error (rest_adapter.js:896)
    at jQuery.Callbacks.fire (jquery.js:3143)
    at Object.jQuery.Callbacks.self.fireWith [as rejectWith] (jquery.js:3255)
    at done (jquery.js:9311)
    at XMLHttpRequest.jQuery.ajaxTransport.send.callback (jquery.js:9713)

I dropped some logging into the article.js adapter, and I see that Ember is passing null into the id argument for buildURL:

 buildURL(type,id,record) called: article, null, <borrowers@model:article::ember741:null>

Is this a change in behaviour with a new Ember Data version, or did I mess something up?

My RESTful instinct here tells me that one should POST new elements to a collection (e.g. /friends/721/articles), not an individual resource (friends/721/articles/1234). (e.g. how is it even supposed to know it's own ID before it's created?)

Version info:

 DEBUG: Ember      : 1.10.0
 DEBUG: Ember Data : 1.0.0-beta.15
 DEBUG: jQuery     : 1.11.2
jbinto commented 9 years ago

Looking at the github repo (https://github.com/abuiles/borrowers/) I don't see the custom article.js adapter actually being used, even in earlier commits.

Was this just for illustrative purposes, to show adapter loading order? If so I would recommend removing it entirely, or at least making it very clear that it's not supposed to be kept.

I am now at Page 66, before adding validation. I'm getting the same error trying to save actually clicking the Save button as I was in the console, above.

Console:

Failed to load resource: the server responded with a status of 404 (Not Found)

Ember server:

POST /friends/725/articles/null 404 186.258 ms - 36
jbinto commented 9 years ago

See #172. ArticleAdapter isn't actually supposed to be implemented.