2012-09-25 18 views
1

我有這個link_to_function我如何把這個link_to_function在軌LINK_TO 3

= link_to_remote 'Populate Info From ID', :url => {:controller => 'something', 
    :action => 'populate_from_id'}, 
    :with => "'id=' + $('account_id').value + '&field_prefix=purchaser'", 
    :update => {:failure => 'account_id_error'} 

我已經轉換很多人在鐵軌與, :remote => true, :method => :post

升級但我不知道如何與加條件來獲取值...任何想法

回答

1

表示回調的所有AJAX特定選項在Rails 3 link_to幫助器中消失了。您必須編寫自己的JavaScript來處理更復雜的遠程操作,例如您的示例。

這裏有一個快速改寫:

# Your template 

= link_to 'Populate Info From ID', :url => {:controller => 'something', 
    :action => 'populate_from_id'}, :id => "#populate_info" 


# In javascript, assuming jquery and 
# an element #account_id with a data-id attribute 
$("#populate_info").on("click", function() { 
    $.ajax({ 
    method: "POST", 
    data: { id: $('#account_id').data("id"), field_prefix: "purchaser" } 
    error: account_id_error 
    }); 
    return false; 
}); 

有用的博客文章:http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/ 很多偉大的文檔在這裏:http://api.jquery.com/jQuery.ajax/

0

你打我吧,我想出了這個

$('.populate_id').click(function(e){ 
    e.preventDefault(); 
    var url = $(this).attr('href'); 
    var failure = $('#' + $(this).attr('failure')); 
    failure.html(''); 
    var element = $('#' + $(this).attr('element')); 
    var id = element.val(); 
    var url_extra = 'account_id=' + id + '&field_prefix=purchaser'; 
    $.ajax({ 
     url: url, 
     data: url_extra, 
     type: 'post', 
     error: function (data) { 
     failure.html(data.responseText); 
     } 
    }); 
    return false; 
});