您要做的是将ajax调用路由到控制器,该控制器将使用包含信息的json进行响应。然后,您将使用jquery填充不同的字段。
在您的路线中:
get "invoice/:id/get_json", :controller=>"invoice", :action=>"get_json"
在您的invoice_controller中:
def get_json invoice = Invoice.find(params[:invoice_id]) render :text => invoice.to_jsonend
在您的帐单模型中(如果默认的to_json方法不足够):
def to_json json = "{" json += "id:'#{self.id}'" json += ",date_created:'#{self.date}'" ... //add other data you want to have here later json += "}"end在您的Javascript文件中,
$("#invoice_selecter").change(function(){ //calls this function when the selected value changes $.get("/invoice/"+$(this).val()+"/get_json",function(data, status, xhr){ //does ajax call to the invoice route we set up above data = eval(data); //turn the response text into a javascript object $("#field_1").val(data.date_created); //sets the value of the fields to the data returned ... });});您可能会遇到一些问题,如果您不在google chrome上,我强烈建议下载并安装fire
bug。如果是,请确保使用的是开发工具。我相信您可以通过右键单击并单击检查元素来打开它们。通过这种方式,您应该能够监视ajax请求,它是否成功以及其他情况。



