0

I'm having trouble using $resource to POST something new... It works great for updates & queries, but when I try and create a new 'article', its getting a little confusing.

Basically my POST should be sent to "/articles/". The ID of the article is set through the form on the front end, however when I submit the POST it is sent to "/articles/ARTICLE_ID"(ARTICLE_ID being replaced with whatever I set on the form).

Here is my article service:

angular.module('testapp.articles')
  .factory("Articles", ['$resource', function($resource) {
    return $resource('articles/:articleId', {
        articleId: '@articleId'
    }, {
        update: {
            method: 'PUT'
        }
    });
}]);

This is my controller:

$scope.create = function() {
  var article = new Articles({
    articleId: this.articleId
  });
  article.$save(function(response){
    console.log(response);
  });
};

Any help is appreciated, thanks!

doostin
  • 237
  • 2
  • 11

1 Answers1

0

It goes off to /articles/ARTICLE_ID because that is what you specified while creating the Article. You do not need to supply article_id as a parameter.

$scope.create = function() {
   var article = new Articles();
   article.articleId = this.articleId
   article.$save();
   ...
};

With the newer versions of AngularJS, articleId will be picked up anyway. Your service could be simplified as follows:

angular.module('testapp.articles')
   .factory("Articles", ['$resource', function($resource) {
      return $resource('articles/:articleId', null,
            {
                'update': { method: 'PUT' }
            });
}]);

AngularJS $resource

kubuntu
  • 2,525
  • 1
  • 22
  • 24
  • Thanks, this helped! Although after I changed it, it did stop posting to /articles/:articleId and started posting to /articles/ but my posts were also getting stuck in a "pending" state, with "CAUTION: Provisional Headers are shown" being displayed in the network panel. so I also had to reset the headers using the method found [here](http://stackoverflow.com/questions/21630534/node-js-angular-js-caution-provisional-headers-are-shown). Its all working now! – doostin Feb 10 '14 at 16:29