我目前正在努力解決如何刪除使用Angular JS和Sinatra的記錄。目前該代碼會引發內部500服務器錯誤。我在網上瀏覽瞭如何正確執行此操作的教程,但找不到任何相關的內容。角JS刪除方法Sinatra
我的代碼如下所示:
app.rb
#Delete download
#not working...
delete '/view1/downloaddelete' do
ng_params = JSON.parse(request.body.read)
@download = Download.get(ng_params)
if @download.destroy
{:success => "ok"}.to_json
#log to console if download delete is successful
puts "download delete successful"
else
halt 500
#log to console if download delete is unsuccessful
puts "download delete unsuccessful halt 500"
end
end
downloads.html
<p>Manage downloads</p>
<ul>
<li ng-repeat="item in items">Title: {{item.title}}, ID: {{item.downloadID}}<a ng-controller="MyCtrl3" ng-click="deletedownload({{item.downloadID}})">Delete</a></li>
</ul>
controllers.js
//not working...
app.controller('MyCtrl3', ['$scope', '$http', function ($scope, $http) {
$scope.downloaddeleteid = {};
$scope.deletedownload = function() {
$http({
method : 'DELETE',
url : '/view1/downloaddelete',
data : $scope.downloaddeleteid
});
}
console.log($scope.deletedownload);
}]);
任何幫助將是非常請欣賞。
非常感謝!非常豐富的答案:)我也必須修改我的ruby方法,請參閱編輯! –