私は以下を試みています。 メインフォームの顧客選択オプション。ユーザーがメインフォームで顧客を選択すると、彼の請求書はすべての後続部分に表示されます。ruby on railの部分データに対してjQueryが実行するデフォルトアクション
私はryan batesのnested_form gemを使ってパーシャルを取得しています。コードは次のとおりです。部分
<div class="bill">
<%= f.collection_select :customer_bill_id, CustomerBill.all,:id,:id, {:prompt => "Select Customer bill"}, :style => 'width:202px;' %></div><br />
/*rest of the code*/
内部
<%= simple_nested_form_for @money_receipt do |f| %>
<div class="customers"><%= f.collection_select :customer_id, Customer.all, :id, :name, options ={:prompt => "-Select a Customer"}, :style=>'width:210px;'%></div><br />
/*rest of the code*/
<%= f.link_to_add "Add line items", :money_receipt_line_items %>
私はコントローラに
jQuery(document).ready(function(){
jQuery(".customers select").bind("change", function() {
var data = {
customer_id: jQuery(this).val()
}
jQuery.getJSON(
"/money_receipts/get_bills",
data, function(data){
var result = "",a,x;
var b = "<option>Select customer Bill</option>"
for(i=0;i<data.length; i++)
{
a = data[i];
result = result + "<option value="+a[0]+">"+a[0]+"</option>";
console.log(result);
}
child=jQuery('.bill select').html(b+result);
});
});
});
、最終的には次のように上記のためのjQueryのコードがあります。 はDEF私は法案をフィルタリングしようとするとのdivのid bill
に入れ
@bill_amount = CustomerBill.find_all_by_customer_id(params[:customer_id]).map{|bill| [bill.id]} if params[:customer_id]
respond_to do |format|
format.json { render json: @bill_amount }
end
end
get_bills。今私はconsole.log
jQueryコードで行う場合、私はcustomer_idと請求書を取得していますが、私はそれを投稿することはできません。もし私がchild
のconsole.log
をすれば、これはjQuery()
として出力されます。どこが間違っていますか?上記を達成する良い方法はありますか?ガイダンスが必要です。前もって感謝します。
ありがとう、これは素晴らしいですが、入れ子になったフォームに上記のメソッドを適用する方法はありますか?もし私が間違っているなら、私はこれをただ一つの部分に当てはめると信じています。 – deeraj