所以我相信這有兩個方面:JavaScript和控制器改變。
在你的控制器,你需要確保它可以返回JSON輸出(或XML或任何你選擇的AJAX-Y輸出):
def man
# do your work
return_data = {}
# initialize your return data
respond_to do |format|
render :json => return_data.to_json, :layout => nil
end
end
有很多種方法來生成JSON輸出,但基本上你有以確保它的形狀很容易在javascript視圖中使用。
我使用jQuery和這裏的執行Ajax調用的代碼:
function foo(some_param) {
$.ajax({
type: 'GET',
url: "/<controller>/man?FOO=" + some_params,
dataType: 'json',
success: handle_success,
error: handle_errors
}
function handle_success(data) {
# process return JSON. it's a javascript object corresponding to the shape
# of your JSON. If your json was a hash server side, it will be an 'object', etc
}
function handle_error(data) {
# handle error cases based upon failure in the infrastructure, not
# failure cases that you encounter due to input, etc.
}
可以配合foo的功能,一些按鈕或作爲的onclick你的願望。
我不確定這是否完整。讓我知道如果你需要更多的細節,等等