Selecting customers and addresses in a form (was Getting joi

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

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

I’m jumping between controller and view several times; alternating
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
of that customer’s addresses.

It works like this:

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


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


<% form_for :invoice, @invoice, :url => { :action => “create” } do |f|
collection_select(:invoice, :customer_id, @customers, :id,
{:include_blank => true},
{:onchange => remote_function(:url => { :action =>
:with => “‘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
    then Rails renders the customer.rjs which in turn inserts customer.rhtml


def get_customer
@customer = Customer.find(params[: invoice_customer_id])
@addresses = Customer.find(params[: invoice_customer_id]).addresses


page[:customer].replace_html :partial => ‘customers/customer’, :object
page[:customer].visual_effect :highlight


    collection_select(: invoice, :address_id, @addresses, :id, 

{:include_blank => true},
:onchange => remote_function(:url => { :action =>
:with => “’ invoice_address_id=’+ $F(‘invoice

  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
to expose the selected customer’s addresses – they are available via
@customer object thanks to the has_many relationship — however, I
figured out how to do that.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs