Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS Resource - PUT method is not getting the id

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!

like image 982
doobdargent Avatar asked Nov 25 '13 23:11

doobdargent


1 Answers

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"
            }
        }
    });
}]);
like image 118
doobdargent Avatar answered Nov 15 '22 22:11

doobdargent