新手在这里尝试在Rails4中创建一个简单的通知提交和删除应用程序。我正在使用Ajax提交/删除,但我不知道为什么我的对象不能删除。
My Controller包含以下Delete方法:
def destroy
Note.find(params[:id]).destroy
render :json => "Note deleted!"
end我的Create方法如下:
def create
note = Note.create(title: note_params[:title], description: "click to add description")
render :json => note
end在我的application.js中,我使用Ajax提交,然后并发地创建一个"Delete“按钮:
$(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;
});有什么想法吗?非常感谢!
-A
发布于 2014-04-22 03:17:37
你应该试试这样的东西:
$('.ajay').live('click', function(){
$.ajax({
url: '/note/id',
type: 'DELETE',
success: function(result) {
// Do something with the result
}
});
});https://stackoverflow.com/questions/23204047
复制相似问题