2016-10-01 69 views
0

我有一個這種形式。Rails窗體助手 - 輸入 - Ajax

<%= form_for @snitch, html: { class: "form-actions", method: 'delete' } do |form| %> 
<span class="button-text"><%= link_to 'NO WAY!', home_base_url_or_default(root_path), rel: "modal:close" %></span> 
<input type="submit" class="button button--modal delete-snitch" data-snitch-id="<% @snitch.token %>" value="Yes, delete it."> 
<% end %> 

正如您在第3行看到的那樣有一種輸入類型。我在它上面有這個屬性data-snitch-id我把它設置成了snitch令牌。我只是想知道,如果這是正確的,因爲它回來未定義...

這是我的知識。

我使用它的Ajax調用

$(document).on('click','.delete-snitch', function(e) { 
    e.preventDefault(); 
    var snitchID = $(this).attr('data-snitch-id'); 
    $.ajax({ 
     type: 'DELETE', 
     url: '/snitches/' + snitchID, 
     datatype: 'json' 
     success: function(){ 
      $('#tr-for-snitch-' + snitchID).fadeOut 
     } 
    }); 
}); 

但它不工作,因爲data-snitch-id回來爲未定義。

回答

0

您必須使用<%= @snitch.token %>而不是<% @snitch.token %>來輸出該值。