Forum: Ruby on Rails RE: Update multiple fields in a form on change event

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
torben.wolm (Guest)
on 2005-11-17 16:17
(Received via mailing list)
Hi Damon,

Do you have some code to show? That would make things a bit easier to
comment on...

Best regards,
torben
mdamonhill (Guest)
on 2005-11-17 16:44
(Received via mailing list)
Torben--

Thanks for the reply.
I do have some code that I posted yesterday in another thread, but would
love to post here again for you :)

Main View

<td><label for="purchase_order_cutomer_address">Customer
Address:</label></td>
<td><div id="po_customer_address"></div></td>
</tr>
<tr>
<td><label for="purchase_order_shipper_address">Ship Via:</label></td>
<td><div id="po_shipper_address"></div></td>
<td>&nbsp;</td>
<td><label for="purchase_order_credit_limit">Credit Limit:</label></td>
<td><div id="po_credit_limit"></div></td>
<td><label for="purchase_order_current_balance">Current
Bal:</label></td>
<td><div id="po_current_balance"></div></td>
</tr>
</table>
<%= observe_field(:purchase_order_customer_id,
:update => :po_customer_address,
:url => { :action => :get_customer_address },
:with => "'customer_id='+escape(value)")%>
<%= observe_field(:purchase_order_customer_id,
:update => :po_credit_limit,
:url => { :action => :get_customer_credit_limit },
:with => "'customer_id='+escape(value)")%>

Controller

def get_customer_address
form_customer_id = params[:customer_id]
@found_customer = Customer.find(form_customer_id)
end

def get_customer_credit_limit
form_customer_id = params[:customer_id]
@found_customer_limit = Customer.find(form_customer_id)
end

Returning Views

get_customer_address.rhtml
<%= text_field("purchase_order", "customer_address", :readonly =>
"readonly", "size" => 25, :value => @found_customer.address) %><br />
<%= text_field("purchase_order", "customer_city", :readonly =>
"readonly",
"size" => 10, :value => @found_customer.city) %>
<%= text_field("purchase_order", "customer_state", :readonly =>
"readonly",
"size" => 2, :value => @found_customer.state) %>
<%= text_field("purchase_order", "customer_postal_code", :readonly =>
"readonly", "size" => 5, :value => @found_customer.postal_code) %>

get_customer_credit_limit.rhtml
<%= text_field("purchase_order", "customer_credit_limit", :readonly =>
"readonly", :value => @found_customer_limit.credit_limit) %>

Thanks!
~damon
torben.wolm (Guest)
on 2005-11-17 21:22
(Received via mailing list)
Hi Damon

I can't say that I have tried the thing you want, but I had a look in
the "Agile Development"-book that holds a tip I think might be usable.

The tip goes, that you return a bunch of generated javascript that you
evaluate in the client.

So, you will do something like:

observe_field(:purchase_order_customer_id,
  :complete => "eval(request.responseText)",
  :url => { :action => :get_all_customer_info },
  :with => "'customer_id='+escape(value)")

In the template, you generate the javascript that updates all the
fields:

document.getElementById("po_customer_address").value = '<%=
@found_customer.address %>';
document.getElementById("po_credit_limit").value = '<%=
@found_customer_limit.credit_limit %>';

and so on.

Hope this helps...

Torben
imobachgs (Guest)
on 2005-11-17 21:37
(Received via mailing list)
El Jueves 17 Noviembre 2005 19:20, Torben
Wölm escribió:> observe_field(:purchase_order_customer_id,
> @found_customer_limit.credit_limit %>';
I think that you have an "update_element_function"[1] to do such a
thing.

Good luck!

[1]
http://api.rubyonrails.org/classes/ActionView/Help...

--
_o)  Imobach González Sosa
//\  email: imobachgs at step dot es
V_/_ jid: osoh at jabberes dot org
url: http://www.banot.net/~osoh/
blog: http://devnull.blogs.banot.net/

-------------------
http://www.step.es/

--
Este mensaje ha sido analizado por STEP On Line
en busca de virus y otros contenidos peligrosos,
y se considera que está limpio. 902 10 18 43
torben.wolm (Guest)
on 2005-11-17 21:46
(Received via mailing list)
>I think that you have an "update_element_function"[1] to do such a thing.
>
>Good luck!

>[1]
>http://api.rubyonrails.org/classes/ActionView/Help...

Ahh - sweet. The documentation even holds an example of -- how to make
multiple updates!

Thanks,
Torben
mdamonhill (Guest)
on 2005-11-17 22:11
(Received via mailing list)
Thanks for the replies Torben and Imobach.
I did attempt to implement the update_element_function however, the
action
it was looking for was an onSubmit() and I need the form to update on an
onEvent() action.

I will look again and see if there is a way to set which action the
update_element_function looks for.

Thanks again,
~damon
mdamonhill (Guest)
on 2005-11-17 23:26
(Received via mailing list)
Solved!!!

The examples and hints given by Torben worked beautifully! Thanks!

Here is my view now:

<tr>
<td colspan="2" align="right"><label
for="purchase_order_cutomer_address">Customer Address:</label></td>
<td colspan="2"><input type="text" id="po_customer_address" ></td>
<td><label for="purchase_order_shipper_address">Shipper
Address:</label></td>
<td><div id="po_shipper_address"></div></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
<td colspan="2"><input type="text" id="po_customer_city"> <input
type="text"
id="po_customer_state"> <input type="text"
id="po_customer_postal_code"></td>
</tr>
<tr>
<td><label for="purchase_order_credit_limit">Credit Limit:</label></td>
<td><input type="text" id="po_credit_limit"></td>
<td><label for="purchase_order_current_balance">Current
Bal:</label></td>
<td><input type="text" id="po_current_balance"></td>
<td><label for="purchase_order_customer_pickup">Customer
Pickup:</label</td>
<td><%= check_box_tag("po_customer_pickup", value="1", checked=false)
%></td>
</tr>

<%= observe_field(:purchase_order_customer_id,
:complete => "eval(request.responseText)",
:url => { :action => :get_customer_info },
:with => "'customer_id='+escape(value)")%>

My controller just finds the specified customer record designated by
customer_id.

Then the get_customer_info.rhtml looks like this:

document.getElementById("po_customer_address").value = '<%=
@found_customer.address %>';
document.getElementById("po_customer_city").value = '<%=
@found_customer.city %>';
document.getElementById("po_customer_state").value = '<%=
@found_customer.state %>';
document.getElementById("po_customer_postal_code").value = '<%=
@found_customer.postal_code %>';
document.getElementById("po_credit_limit").value = '<%=
@found_customer.credit_limit %>';
document.getElementById("po_current_balance").value = '<%=
@found_customer.inv_balance %>';

Thanks again!
This topic is locked and can not be replied to.