2016-08-01 3 views
0

js.erb 파일에서 컨트롤러 메서드 (레코드 삭제 용)를 호출하려고했습니다. 내가 가진 것 :레일에서 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

레일스 방식으로'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을 사용하고 있다고 가정합니다.

실제로이 방법은 올바른 방법이 아닙니다. 아약스 렌더링을 사용해야합니다.

관련 문제