{"action":"created","issue":{"url":"https://api.github.com/repos/js-data/js-data-http/issues/16","labels_url":"https://api.github.com/repos/js-data/js-data-http/issues/16/labels{/name}","comments_url":"https://api.github.com/repos/js-data/js-data-http/issues/16/comments","events_url":"https://api.github.com/repos/js-data/js-data-http/issues/16/events","html_url":"https://github.com/js-data/js-data-http/issues/16","id":90622657,"number":16,"title":"Handling Rails HEAD 204 on update","user":{"login":"kaisersly","id":1589912,"avatar_url":"https://avatars.githubusercontent.com/u/1589912?v=3","gravatar_id":"","url":"https://api.github.com/users/kaisersly","html_url":"https://github.com/kaisersly","followers_url":"https://api.github.com/users/kaisersly/followers","following_url":"https://api.github.com/users/kaisersly/following{/other_user}","gists_url":"https://api.github.com/users/kaisersly/gists{/gist_id}","starred_url":"https://api.github.com/users/kaisersly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kaisersly/subscriptions","organizations_url":"https://api.github.com/users/kaisersly/orgs","repos_url":"https://api.github.com/users/kaisersly/repos","events_url":"https://api.github.com/users/kaisersly/events{/privacy}","received_events_url":"https://api.github.com/users/kaisersly/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":10,"created_at":"2015-06-24T09:26:16Z","updated_at":"2015-09-30T02:21:06Z","closed_at":"2015-06-24T16:15:36Z","body":"# The problem\r\nRails default response to an update is :\r\n```ruby\r\n\thead :no_content\r\n```\r\nwhich is an empty response with a status code of 204 (NO CONTENT).\r\n\r\nEven if you write :\r\n```ruby\r\n\tdef update\r\n\t\tif @resource.update(params)\r\n\t\t\trender @resource, status: :ok\r\n\t\tend\r\n\tend\r\n```\r\nyou'll still get an empty response (HEAD) with a 204 status code.\r\n\r\nWhen you do a `DS.find('resource', id)`, js-data will send a PUT request. It will then receive a HEAD 204 response and will try to inject the payload in the cache. So it will perform something like :\r\n```javascript\r\n\tDS.inject('resource', JSON.parse('')) // the '' comes from the empty payload in the HEAD response\r\n```\r\nand will throw an error (you can't inject an empty object).\r\n\r\n# The Rails solution\r\nThis stackoverflow [question](http://stackoverflow.com/questions/9953887/simple-respond-with-in-rails-that-avoids-204-from-put) provides the Rails way to change the response.\r\n\r\n# The js-data solution\r\n## Step 1 : disable the injection\r\nYou can disable the injection when you update a resource.\r\nYou can disable it app wide :\r\n```javascript\r\n\tvar DS = new JSData.DS({\r\n\t\tbeforeUpdate = function (resource, data, cb) {\r\n\t\t\tresource.cacheResponse = false;\r\n\t\t\tcb(null, data);\r\n\t\t};\r\n\t});\r\n```\r\nor resource wide :\r\n```javascript\r\n\tvar Resource = DS.defineResource({\r\n\t\tname: 'resource',\r\n\t\tbeforeUpdate = function (resource, data, cb) {\r\n\t\t\tresource.cacheResponse = false;\r\n\t\t\tcb(null, data);\r\n\t\t};\r\n\t});\r\n```\r\nIf you use Angular (app wide) :\r\n```javascript\r\nangular.module('myApp', ['js-data'])\r\n\t.config(function (DSProvider) {\r\n\t\tDSProvider.defaults.beforeUpdate = function (resource, data, cb) {\r\n\t\t\tresource.cacheResponse = false;\r\n\t\t\tcb(null, data);\r\n\t\t};\r\n\t});\r\n```\r\n\r\n## Step 2 : inject the resource manually\r\nYou just disabled the injection. If you stop here, this will happens :\r\n```javascript\r\n\tvar myResource = DS.get('resource', 1);\r\n\t// { name: 'my resource' }\r\n\tmyResource.name = 'a new name'\r\n\tmyResource.DSSave();\r\n\r\n\tmyResource = DS.get('resource', 1);\r\n\t// still { name: 'my resource' } because the updated resource wasn't injected\r\n```\r\nYou have to inject the resource when the update is successful :\r\n```javascript\r\n\tvar myResource = DS.get('resource', 1);\r\n\t// { name: 'my resource' }\r\n\tmyResource.name = 'a new name'\r\n\tmyResource.DSSave().then(function (data) { // data, the payload of the response, is empty\r\n\t\tDS.inject('resource', myResource);\r\n\t});\r\n\r\n\tmyResource = DS.get('resource', 1);\r\n\t// { name: 'a new name' }\r\n```\r\n\r\n=============\r\n\r\n_original question_\r\n\r\nI'm using Rails as my backend. It responds to successful PATCH/PUT request with a HEAD 204 (NO CONTENT).\r\n\r\nWhen js-data gets the response, the data.data properties is \"\" and it triggers an error when js-data tries to inject it.\r\n\r\nWhat is the proper way to handle this case (apart from tweaking Rails behaviour) ?"},"comment":{"url":"https://api.github.com/repos/js-data/js-data-http/issues/comments/144261190","html_url":"https://github.com/js-data/js-data-http/issues/16#issuecomment-144261190","issue_url":"https://api.github.com/repos/js-data/js-data-http/issues/16","id":144261190,"user":{"login":"jmdobry","id":1213510,"avatar_url":"https://avatars.githubusercontent.com/u/1213510?v=3","gravatar_id":"","url":"https://api.github.com/users/jmdobry","html_url":"https://github.com/jmdobry","followers_url":"https://api.github.com/users/jmdobry/followers","following_url":"https://api.github.com/users/jmdobry/following{/other_user}","gists_url":"https://api.github.com/users/jmdobry/gists{/gist_id}","starred_url":"https://api.github.com/users/jmdobry/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmdobry/subscriptions","organizations_url":"https://api.github.com/users/jmdobry/orgs","repos_url":"https://api.github.com/users/jmdobry/repos","events_url":"https://api.github.com/users/jmdobry/events{/privacy}","received_events_url":"https://api.github.com/users/jmdobry/received_events","type":"User","site_admin":false},"created_at":"2015-09-30T02:21:06Z","updated_at":"2015-09-30T02:21:06Z","body":"@itsmechlark This should work just fine:\r\n\r\n```javascript\r\nvar Foo = DS.defineResource({\r\n  name: 'foo',\r\n  cacheResponse:false\r\n});\r\n\r\nvar foo = Foo.inject({ id: 1, name: 'bar' });\r\nfoo.name; // \"bar\"\r\nfoo.name = 'beep';\r\nfoo.name; // \"beep\"\r\nfoo.DSSave().then(function () {\r\n  foo.name; // \"beep\"\r\n  Foo.get(1).name; // \"beep\"\r\n  foo.name === Foo.get(1).name; // true\r\n  foo === Foo.get(1); // true\r\n});\r\n```"}}