[英]Unable to delete object in Rails using Ajax
Noob在這里嘗試在Rails 4中創建一個簡單的便箋提交和刪除應用程序。我正在使用Ajax提交/刪除,但是無法弄清楚為什么我的對象不會刪除。
我的控制器包括以下Delete方法:
def destroy
Note.find(params[:id]).destroy
render :json => "Note deleted!"
end
我的“創建”方法如下:
def create
note = Note.create(title: note_params[:title], description: "click to add description")
render :json => note
end
在我的application.js中,我正在使用Ajax提交,然后同時創建一個“刪除”按鈕,如下所示:
$(document).ready(function(){
$("form").submit(function(){
$.post(
$(this).attr('action'),
$(this).serialize(),
function(data){
console.log('Data Received from the Ajax call', data);
//put additional codes here to update html, etc.
//for example things like
$('.all-notes').append(data.title);
$('.all-notes').append('<input type="button" class="ajay" value="Delete">')
},
"json"
);
return false;
});
但是,當我提交便箋時,我看到帶有相關刪除按鈕的便箋,但實際上單擊該按鈕沒有任何作用。 這是在application.js文件中用於刪除的我的Ajax代碼:
$(".ajay").submit(function(){
$.post(
$(this).attr('action'),
$(this).serialize(),
function(data){
console.log('Data Received from the Ajax call', data);
//put additional codes here to update html, etc.
//for example things like
$('.all-notes').remove(data.title);
},
"json"
);
return false;
});
有什么想法嗎? 非常感激!
-一種
您應該嘗試這樣的事情:
$('.ajay').live('click', function(){
$.ajax({
url: '/note/id',
type: 'DELETE',
success: function(result) {
// Do something with the result
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.