2012-06-19 43 views
0
## NO-AJAX, No filtering 
# Since there is no filtering, it lists all the orders. 
## 
# partial: 
<%= f.collection_select(:customer, @customers, 
    :customer_id, :customer_name, {:prompt=>'Select a Customer'}) 
%> 
<%= f.collection_select(:order_id, @orders, 
    :order_id, :order_name, {:prompt=>'Select an Order'}) 
%> 

# view: 
<%= nested_form_for @service do |f| %> 
    <%= f.link_to_add "Add Order", :orders %> 
    <%= f.submit "Submit" %> 
<% end %> 

#controller: 
def new 
    @service = Service.new 
    @service.orders.build 
    @customers = Customer.find(:all) 
    @orders = Order.find(:all) 
end 


## Q: BUT I WANT TO DO filtering based on the customer select, with AJAX and update the div accordingly. 
# partial: 
<%= f.collection_select(:customer, @customers, 
    :customer_id, :customer_name, {:prompt=>'Select a Customer'})%> 
<div id='display'></div> 

這裏的每一個link_to_add獨特div編號是我的問題:每link_to_add,我想創造出獨特的「顯示」分區,因爲我有填充的是「顯示」 DIV的一個javascript該collection_select的onchange事件。 感謝您的幫助!在一個nested_form

回答

0

如果我正確理解你的問題,下面的代碼將創建獨特的div每次你點擊鏈接。

HTML

<a href="#" id="link_to_add">Link to Add</a> 

​<div id="parent_div"> 
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

jQuery的

var divCnt = 1; 
$(document).ready(function() { 
    $('#link_to_add').click(function(){ 
     $('#parent_div').html($('#parent_div').html()+'<div id="display_'+divCnt+'">Display div '+divCnt+'</div>'); 
     divCnt++; 
    }); 
}); 

希望這有助於!