Fleeck
Fleeck

Reputation: 1066

DELETE not found; rails + angular

CODE:

Rails controller:

def destroy
  respond_with Post.find(params[:id]).destroy
end

Angular factory:

.factory('posts', [
  '$http',
  function($http) {
    var o = {
      posts: []
    };
    o.destroy = function(post) {
      return $http.delete('posts.json', post).success(function(data) {
        o.posts.splice(o.posts.indexOf(data), 1);
      });
    };
    return o;
  }
]);

Part of a main module:

angular.module('flapperNews', ['ui.router', 'templates', 'Devise'])
.config([
  '$stateProvider',
  '$urlRouterProvider',
  function($stateProvider, $urlRouterProvider) {

   $stateProvider
    .state('home', {
      url: '/home',
      templateUrl: 'home/_home.html',
      controller: 'MainCtrl',
      resolve: {
        postPromise: ['posts', function(posts) {
          return posts.getAll();
        }]
      }
    })
  }
 ]);

mainCtrl.js:

angular.module('flapperNews')
.controller('MainCtrl', [
'$scope',
'posts',
function($scope, posts) {
  $scope.posts = posts.posts;
  ...
  $scope.destroy = function(post) {
    posts.destroy(post);
  };
}
]);

PROBLEM: When trying to click on delete button ng-click="destroy(post)" - it doesn't work and gives me the error:

DELETE http://localhost:3000/posts.json 404 (Not Found)

The interesting part is when I change my $http.delete to $http.post - it works, but on refresh I have 2 same posts(obviously). I am a newbie in both angular and rails, so I would really appreciate your help.

Rake routes: Rake routes

Upvotes: 0

Views: 165

Answers (2)

Fleeck
Fleeck

Reputation: 1066

Okay, I got this. The problem was in this row: return $http.delete('posts.json', post). Need to change this to return $http.delete('/posts/' + post.id + '.json'), as rake routes says it should be.

Upvotes: 1

Jim Edelstein
Jim Edelstein

Reputation: 792

Make sure you have resources :posts in your rails routes file.

Upvotes: 1

Related Questions