2013-09-26 24 views
0

我使用UJS填充模式內的窗體,我使用JQuery Validate執行驗證並通過ajax將表單提交回服務器。Rails 4 UJS不發射失敗/完成/總是回調

一切都很好,窗體得到渲染,字段驗證,表單被正確提交到服務器。問題是掛起$.rails.handleRemote函數的回調不會觸發。

這裏是CoffeeScript中,我使用的驗證和提交(此功能是從與new.js.erb和edit.js.erb叫:

@initializeForm: -> 
    $(".note-form").validate -> 
     rules: 
      "note[title]" : "required" 
    submitHandler: (form) -> 
     $.rails.handleRemote($(form)) 
      .done -> 
       alert "You will never see me!" 

new.js.erb

$('#modal-window').html('<%= escape_javascript(render :partial => 'form', :object => @note) %>'); 

Note.initializeForm(); 

create.js.erb

var note = new Note(); 

note.createOrUpdate("note<%= @note.id %>", "<%= @note.title %>", "<%= escape_javascript(render @note) %>"); 

我能夠通過在應用廣泛的腳本添加此周圍破解:

$(document).on 'ajax:success', '.note-form', -> 
    alert "Here I am!" 

我已經驗證了我只有裝的JQuery的一個副本。

任何想法都會很棒!

編輯 這裏是發送到瀏覽器編譯後的JS:

Note.initializeForm = function() { 
    return $(".note-form").validate(function() { 
     return { 
      rules: { 
       "note[title]": "required" 
      }, 
      submitHandler: function(form) { 
       return $.rails.handleRemote($(form)).always(function() { 
        return alert("You will never see me!"); 
       }); 
      } 
     }; 
    }); 
}; 
+0

服務器端代碼進行故障排除客戶端(JavaScript)代碼問題實際上是無用的。向我們展示_rendered_ JavaScript和HTML代碼......換句話說,就像瀏覽器所看到的那樣。 – Sparky

回答

0

的問題是與調用validate的語法。我傳遞的是一個函數而不是一個對象。

正確的方法來調用validate從CoffeeScript的:

@initializeForm: -> 
    $(".note-form").validate 
     rules: 
      "note[title]" : "required" 
     submitHandler: (form) -> 
      $.rails.handleRemote($(form)) 
       .done -> 
        alert "You will never see me!"