2011-06-29 28 views
0

到Rails 3之前,我用這個代碼,以動態觀察的字段和在該領域傳遞數據到控制器更改:從的observe_field到JQuery的Rails中3

<%= observe_field :transaction_borrower_netid, 
    :url => { :controller => :live_validations, :action => :validate_borrower_netid }, 
    :frequency => 0.5, 
    :update => :borrower_netid_message, 
    :with => "borrower_netid" %> 

我試圖更新代碼與Jquery和Rails 3一起工作,但我無法實現它的工作。我更新了我的的routes.rb包括

match "/live_validations/validate_borrower_netid" => "live_validations#validate_borrower_netid", :as => "validate_borrower" 

,我想觀察現場,並進行必要的呼叫:

jQuery(function($) { 
    // when the #transaction_borrower_netid field changes 
    $("#transaction_borrower_netid").change(function() { 
    // make a POST call and update the borrower_netid_message with borrower_netid 
    $.post(<%= validate_borrower_path %>, this.value, function(html) { 
     $("#borrower_netid_message").html(html); 
    }); 
    }); 
}) 

,但它不工作。我的Javascript和jquery技能嚴重缺乏,所以任何人可以提供的幫助將不勝感激。謝謝!

回答

2

我最終使用下列內容:

$('#transaction_borrower_netid').live('change', function() { 
// when the #transaction_borrower_netid field changes 
// make a POST call and replace the content 
     var netID = $('#transaction_borrower_netid').val(); 
     $.post("/live_validations/validate_borrower_netid", { borrower_netid: $('#transaction_borrower_netid').val() }, function(html) { 
      $("#borrower_netid_message").html(html); 
     }); 
}) 
2

你需要用你的<%= validate_borrower_path %>引號:

$.post("<%= validate_borrower_path %>", this.value, function(html) { 
+0

感謝您的答覆。我做了改變,但這並沒有解決問題。 –