AngularJS Resource - PUT method is not getting the id

doobdargent picture doobdargent · Nov 26, 2013 · Viewed 9.8k times · Source

I have a factory that returns the $resource for my Article model:

angular.module('ADI.Resources').factory("Articles", ['$resource', function($resource) {
    return $resource('/api/v1/article/:articleId', {
        articleId: '@_id',
        _shop: window.user._shop
    }, {
        update: {
            method: 'PUT'
        }
    });
}]);

GET, POST and DELETE are working well, but not the update (PUT). Here is the code I use:

Articles.update({articleId: '300000000000000000000001'}, function(article){
    console.log(article);
});

It's making this request:

PUT http://localhost:3000/api/v1/article?_shop=100000000000000000000001

instead of:

PUT http://localhost:3000/api/v1/article/300000000000000000000001?_shop=100000000000000000000001

Any idea why the :articleId parameter is not filled when doing an update? Thanks!

Answer

doobdargent picture doobdargent · Nov 26, 2013

As Fals mentionned in the comment, the articleId parameter was in the request content. So I made a little trick to have it also on the URI.

angular.module('ADI.Resources').factory("Articles", ['$resource', function($resource) {
    return $resource('/api/v1/article/:articleId', {
        articleId: '@_id',
        _shop: window.user._shop
    }, {
        update: {
            method: 'PUT',
            params: {
                articleId: "@articleId"
            }
        }
    });
}]);