あなたがしたいことは、ajax呼び出しをコントローラにルーティングすることです。コントローラは、情報を含むjsonで応答します。 jqueryを使用してさまざまなフィールドに値を設定します。あなたのinvoice_controllerで
get "invoice/:id/get_json", :controller=>"invoice", :action=>"get_json"
:
def get_json
invoice = Invoice.find(params[:invoice_id])
render :text => invoice.to_json
end
請求書のモデルでは(デフォルトのto_jsonをする方法がsufficentされていない場合):あなたのルートで
で
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 ..でない場合は、火災のバグをダウンロードしてインストールすることをお勧めしますし、もしあなたが開発ツールを使用していることを確認します。右クリックしてinspect要素を押すことでそれらを開くことができると信じています。これにより、ajaxリクエストと、それが成功したかどうか、そして何かを監視することができます。
あなたが4年前に書いたコードを見て、あなたがしていたことでちょうど恐ろしいでしょうか? – BananaNeil