# Selecting customers and addresses in a form (was Getting joi

**URL:** <https://www.ruby-forum.com/t/selecting-customers-and-addresses-in-a-form-was-getting-joi/99231>\
**Category:** Rails\
**Created:** [July 21, 2006, 5:08am UTC](https://www.ruby-forum.com/t/selecting-customers-and-addresses-in-a-form-was-getting-joi/99231 "2006-07-21T05:08:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [July 21, 2006, 5:08am UTC](https://www.ruby-forum.com/t/selecting-customers-and-addresses-in-a-form-was-getting-joi/99231/1 "2006-07-21T05:08:47Z")

</div>

(Sorry if you have received this twice, I’m having trouble sending mail  
to  
the rails list)

Greetings, I’m looking for suggestions on how to simplify getting  
customer  
addresses on a form:

I’m jumping between controller and view several times; alternating  
between  
rhtml and rjs, in order to display customers and their addresses on an  
invoicing form. It’s very messy.

I have a one to many relationship between Customer and Addresses, each  
customer has one or more addresses. The user opens the Invoice form and  
selects from a list of customers which in turn populates and displays a  
list  
of that customer’s addresses.

It works like this:

1. The Invoice controller receives a new request and renders the  
new.rhtmlform:

/invoices/new

def new  
@invoice = Invoice.new  
@customers = Customer.find(:all, :order =\> “name”)  
end

new.rhtml

\<% form\_for :invoice, @invoice, :url =\> { :action =\> “create” } do |f|  
%\>  
Customer:  
\<%=  
collection\_select(:invoice, :customer\_id, @customers, :id,  
:name,  
{:include\_blank =\> true},  
{:onchange =\> remote\_function(:url =\> { :action =\>  
“get\_customer”},  
:with =\> “‘invoice\_customer\_id=’+  
$F(‘invoice\_customer\_id’)”)}  
)  
%\>

  
\<%= hidden\_field :invoice, :customer\_id, :value =\> “” %\>  
\<%= submit\_tag ‘Add Invoice’ %\>  
\<% end %\>
1. The onchange event calls the controller action get\_customer, which  
does a  
find on the selected customer\_id. It also gets the address collection  
and  
then Rails renders the customer.rjs which in turn inserts customer.rhtml

/invoices/get\_customer/1

def get\_customer  
@customer = Customer.find(params[: invoice\_customer\_id])  
@addresses = Customer.find(params[: invoice\_customer\_id]).addresses  
end

customer.rjs

page[:customer].replace\_html :partial =\> ‘customers/customer’, :object  
=\>  
@customer  
page[:customer].visual\_effect :highlight

customer.rhtml

```
<%=
    collection_select(: invoice, :address_id, @addresses, :id, 

```

:street,  
{:include\_blank =\> true},  
{  
:onchange =\> remote\_function(:url =\> { :action =\>  
“get\_address”},  
:with =\> “’ invoice\_address\_id=’+ $F(‘invoice  
\_address\_id’)”)  
})  
%\>

1. Next a similar pattern occurs for addresses…

Back we go to the controller with the action get\_address and then render  
address.rjs and then address.rhtml . It’s annoying because I have other  
values to gather on this form that also have multiple joins.

I suspect the culprit of this complexity is the onchange event and  
remote\_function calls. I’ve considered calling a local javascript  
function  
to expose the selected customer’s addresses – they are available via  
the  
@customer object thanks to the has\_many relationship — however, I  
haven’t  
figured out how to do that.

Possibly a entirely different solution exists. Your thoughts and ideas  
are  
gratefully appreciated!  
–Dave
