Rails 3 uses UJS approach, which allows to handle AJAX responses from JavaScript code.
$("form").live("ajax:success", function(data, status, xhr){
$(".target").html(data.myHTML);
});
How view in Rails 3 should look like to fill "data" object, so i can inspect is from JavaScript ?
Regular rendering, using "js.erb", or "js.haml" results "data" local variable in JavaScript "undefined".
Of course, in the view, i can use some global variable, like following ("js.haml"):
-开发者_如何学Go c = escape_javascript(render "search_result")
:plain
var ajaxResponse = "#{c}";
… And call it from JavaScript handler, like this:
$("form").live("ajax:success", function(data, status, xhr){
$(".target").html(ajaxResponse);
});
… but it breaks incapsulation.
Ideal way would be using "data" local variable.
So, how the view should look like ?
精彩评论