[英]Calling Controller method from js.erb in rails
我一直在尝试从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方法。 请让我知道如何解决此问题。 谢谢!
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
。
顺便说一句,这不是正确的方法。 您应该使用ajax渲染。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.