我一直试图从js.erb文件中调用控制器方法(删除记录)。我有什么:
var selection = confirm("Are you sure that you want to delete the record?");
if(selection == true){
new Ajax.Request('/students/destroy', {
method: 'post',
parameters: {id: "#{student.id}"}
});
alert("deleted");
}
当用户点击'删除'时,会调用此delete.js.erb。按钮。当用户确认删除时,我试图从js.erb调用destroy方法。请让我知道如何解决这个问题。谢谢!
答案 0 :(得分:0)
if(selection == true){
var student_id = <%= student.id %>;
$.ajax({
type: "DELETE",
url: "/students/"+ student_id +"/destroy", # instead use the destroy path like <%= student_path(student) %>
success: function (result) {
window.alert("success!!");
},
error: function (){
window.alert("something wrong!");
}
});
}
您的类型必须为delete
。我想你正在使用Jquery
。