2016-08-01 67 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渲染。

相關問題