Hello.
I have a form that needs to be different depending on if the customer
being created is a person or organization. I''m just prototyping the
application right now, so most of the actual methods etc. aren''t
implemented yet.
What I wish to do is have two radio-buttons with Person/Organization -
and an AJAX call to instantly show the right form based on the radio
button you click.
I can''t figure out how to do it. Right now I have two stupid, ugly
link_to_remote links like so:
*************
<% @heading = "New Customer" %>
<%= javascript_include_tag "prototype" %>
<%= error_messages_for ''customer'' %>
<p><%= link_to_remote ("Customer is a person",
:update => ''formdiv'',
:url => { :action => :form_person } ) %>
</p>
<p><%= link_to_remote ("Customer is an organization",
:update => ''formdiv'',
:url => { :action => :form_organization } ) %>
</p>
<div id="formdiv"> </div>
*************
the actions just do render(:layout => none) and hence load the
corresponding .rhtml view file into the div. That works just fine.
My question then - how can I run the ajax calls based on the (as of yet
non-existant) radio buttons? And preferrably move the link_to_remote
calls into a controller method to clean up the code somewhat?
On Wed, 7 Sep 2005, Henning Kilset Pedersen wrote:> My question then - how can I run the ajax calls based on the (as of yet > non-existant) radio buttons? And preferrably move the link_to_remote > calls into a controller method to clean up the code somewhat?You can tie the ajax calls to the event handlers (like onClick) of those buttons. Just take a look at the javascript link_to_remote is producing and copy it over to that handler. It looks something like onClick="new Ajax.Updater ... ". However, AJAX is not necessarily needed for this. You can just create two forms on the page in the first place and hide one of them with css. Then you can use the onclick handler to toggle between those two forms. No server interaction needed. //jarkko> > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi
On Wed, 7 Sep 2005, Jarkko Laine wrote:> On Wed, 7 Sep 2005, Henning Kilset Pedersen wrote: > >> My question then - how can I run the ajax calls based on the (as of yet >> non-existant) radio buttons? And preferrably move the link_to_remote >> calls into a controller method to clean up the code somewhat? > > You can tie the ajax calls to the event handlers (like onClick) of those > buttons. Just take a look at the javascript link_to_remote is producing and > copy it over to that handler. It looks something like onClick="new > Ajax.Updater ... ". > > However, AJAX is not necessarily needed for this. You can just create two > forms on the page in the first place and hide one of them with css. Then you > can use the onclick handler to toggle between those two forms. No server > interaction needed.example: <input ... onclick="Element.hide(''first-form''); Element.show(''second-form''); return false;"> <input ... onclick="Element.hide(''second-form''); Element.show(''first-form''); return false;"> <div id="first-form"><form ... </div> <div id="second-form" style="display: none;"><form ... </div> The Prototype library brings you these (and a whole bunch of other) goodies. Take a look at its docs in http://wiki.script.aculo.us/scriptaculous/show/Prototype //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi