Topic: Copying text_field values without reloading view

Hi there,

I have a table of Clients, each client has 2 addresses (delivery and invoice).

I want the user to be able to click on a link 'Use Delivery Address for Invoicing' (or an image)  to copy the delivery address text_field value to the invoice address text_field.

I've tried field_observer and form_observer, and even link_to_remote, but I can't figure out how to read the values that the user has typed on the Delivery address side of the form.

I've also tried Form.serialize('form_id'), but could not get to any of the paramaters on the controller.

If it's not too much trouble, could an enlightened soul give an example of how to use all 3 above in order to read parameters from a form and do something with it without reloading the page.

#view
<%= start_form_tag ( {:action => 'update', :id => @client}, :multipart => false, :id => 'my_form' ) %>

<label for="client_name">Name:</label>
<%= text_field 'client', 'name'  %>

<label for="client_delivery_address">Delivery Address:</label>
<%= text_field 'client', 'delivery_address'  %>

<label for="client_invoice_address">Invoice Address:</label>
<%= text_field 'client', 'invoice_address'  %>

<p><%= submit_tag 'Update' %>

<%= end_form_tag %>

# tried the following to no avail...
<%= link_to_remote 'Copy Delivery Address', :url => {:action => 'copy_delivery_address'},
                            :with => "Form.serialize('my_form')" % -->


<%= observe_form 'my_form', :frequency => 0.5,
      :update => {},
      :url => { :action => 'copy_delivery_address' } %>


# Controller
def copy_delivery_address
  # Tried the following ways to retrieve the parameters
  #@invoice_address = params['client_delivery_address'] # didn't work
  #@invoice_address = @params['client_delivery_address'] # didn't work
  #@invoice_address = params[:delivery_address] # didn't work
  #@invoice_address = @params[:delivery_address] # didn't work
  #@invoice_address = params['delivery_address'] # didn't work
  #@invoice_address = @params['delivery_address'] # didn't work
  #@invoice_address = 'test value - works!'
end

# RJS template
page['client_invoice_address'].value = @invoice_address


Thank you for your help in advance,

Fabricio

Re: Copying text_field values without reloading view

Well, I eventually managed to get observe_form to work, this is what I did...

#view

<%= start_form_tag ( {:action => 'update', :id => @client}, :multipart => false, :id => 'my_form' ) %>

<label for="client_name">Name:</label>
<%= text_field 'client', 'name'  %>
   
<label for="client_delivery_address">Delivery Address:</label>
<%= text_field 'client', 'delivery_address'  %>
 
<label for="client_invoice_address">Invoice Address:</label>
<%= text_field 'client', 'invoice_address'  %>
 
<p><%= submit_tag 'Update' %>
 
<%= end_form_tag %>
 
# observe_form
<%= observe_form 'my_form',
      :url => { :action => 'copy_delivery_address' },
      :with => "Form.serialize(my_form)" %>


# Controller
...
def copy_delivery_address
  # For some reason the parameters were all bundled inside :client
  # Not sure why being new at ruby on rails...

  client = params[:client]
  @invoice_address = client[:delivery_address]
end
...


# RJS template
page['client_invoice_address'].value = @invoice_address

Picked up a new tip that could be useful for other people (sorry if this is common knowledge and I was the only one out of the loop... :-)    )

Make sure you can see the view in your browser, that is the form displays correctly and the information is copied ok from one field to the other.

then, Go to the logs directory and make a copy of your development.log (as accessing it directly didn't work on Windows XP...). Open the copy of the development file and scroll down to the very bottom.

In my case, it looks like this:
Processing ClientsController#copy_delivery_address (for 127.0.0.1 at 2006-11-30 16:50:51) [POST]
  Session ID: e780bde5ff15cdf9bbcc785eaa9fcda2
  Parameters: {"commit"=>"Update", "client"=>{"delivery_address"=>"Bobs House", "invoice_address"=>"Bobs House"}, "Form.serialize(my_form)"=>"client[name]", "action"=>"copy_delivery_address", "controller"=>"clients"}
Rendering clients/copy_delivery_address
Completed in 0.01000 (100 reqs/sec) | Rendering: 0.01000 (100%) | DB: 0.00000 (0%) | 200 OK [http://localhost/clients/copy_delivery_address]


The interesting bit (for me) was that I could tell whether or not parameters where being passed to my method copy_delivery_address (on my controller). It also told me how to access the parameters as they all seem to be an array inside the variable client.

Hope this helps someone,

This is not the exact effect I want, so I'll try field_observer and link_to_remote now to see if I can do it.

fabricio.

Re: Copying text_field values without reloading view

The same thing using link_to_remote

Keeping the form_tags and the rest of the view the same (remove the observe_form tag, of course)

The controller and RJS template are kept the same also

# In the View
<%= link_to_remote '(Copy from Delivery)', :url => {:action => 'copy_delivery_address'},
                            :with => "Form.serialize(my_form)" %>

Now, whenever the user clicks on the link (Copy from Delivery) the address from Delivery text_field is copied across to the invoice address text_field.

Fabricio.