2016-08-01 20 views
0

我一直在试图从js.erb文件调用一个控制器方法(删除一条记录)。我有什么:当用户点击“删除”按钮从js.erb在rails中调用控制器方法

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销毁方法。请让我知道如何解决这个问题。谢谢!

+0

为什么不只是使用rails方式'remote:true'?或者你必须使用jQuery? – mrvncaragay

+0

你用<℅= @ student.id>试过了吗?什么是你的控制器变量? – tworitdash

回答

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

顺便说一句,这不是正确的做法。你应该使用ajax渲染。