I stuck on this for the whole day.
On http://localhost:3001/posts/, I list all posts I have in the database. I am trying to delete a task with using Angularjs - every loaded record from the database has a link for its deleting:
= link_to 'Delete', '', 'ng-click' => "deletePost('#{post.id}')", :id => "post_#{post.id}"
Then I have a file /assets/javascript/angular/controller/posts.js that looks like this:
var app = angular.module('AngTestApp', ['ngResource']);
app.factory('Post', function($resource) {
return $resource("/posts/:id", { id: '@id' }, {
index: { method: 'GET', isArray: true, responseType: 'json' },
show: { method: 'GET', responseType: 'json' },
update: { method: 'PUT', responseType: 'json' }
});
})
app.controller("PostsCtrl", function($scope, Post) {
$scope.posts = Post.index();
$scope.deletePost = function(index) {
console.log("index: "+index);
post = $scope.posts[index];
Post.delete(post);
$scope.posts.splice(index, 1);
console.log('aa');
}
})
When I click the delete link, I receive this error in the JS console:
DELETE http://localhost:3001/posts 404 (Not Found)
and in terminal:
Started DELETE "/posts" for 127.0.0.1 at 2015-10-15 16:23:08 -0700
ActionController::RoutingError (No route matches [DELETE] "/posts"):
The routes look like this:
resources :posts do
resources :comments
end
The JS code is very chaotic, I tried to modify a tutorial I found on the Internet, but it doesn't work well.
What is wrong in this attempt?
Thank you in advance.