Hi, I''d like to create a select box with names that automatically update some text on the same page whenever a name is selected. The name and text would come from a record in another table (the id of that record the value of the select box). I understand this can be done using observe_field and related things, however I can''t find any information on how to get it all to work. If someone could help out, point me to an appropriate howto, tutorial or manual page I''d appreciate it! Thanks Mark
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Mark Paxton wrote:> Hi, > > I''d like to create a select box with names that automatically update > some text on the same page whenever a name is selected. The name and > text would come from a record in another table (the id of that record > the value of the select box). > > I understand this can be done using observe_field and related things, > however I can''t find any information on how to get it all to work.I had trouble getting this to work. Here''s a snippet of code I used for the select box. I use the eval to evaluate the code sent back from the server to update the various fields. I''m not sure if I can explain it at the moment... It might rely on some patches that I have been submitting for send_ajax and such. <%= collection_select(''plan'', ''name'', @plans, ''id'', ''name'', {:include_blank => true}, {:onchange => remote_function( :url => { :action => "get_plan_with_ajax", :id => @customer.id}, :with => "Form.Element.serialize(this)", :method => "''post''", :loading => ''$(\''mem_ajax_status\'').innerHTML = \''loading ...\'''', :complete => ''eval(request.responseText);$(\''mem_ajax_status\'').innerHTML = \'' \'''' )}) %> - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCdGn+SIxC85HZHLMRAuN3AKCFS+YoImsyNd+LvGU4WaHumhGmoACfbqG+ iNNkglhRr9sJWhzI43g4XtQ=sgwM -----END PGP SIGNATURE-----
David Morton wrote:> Mark Paxton wrote:[snip]> I had trouble getting this to work. Here''s a snippet of code I used for > the select box. I use the eval to evaluate the code sent back from the > server to update the various fields. I''m not sure if I can explain it > at the moment... It might rely on some patches that I have been > submitting for send_ajax and such. > > <%= collection_select(''plan'', ''name'', @plans, ''id'', ''name'', > {:include_blank => true}, > {:onchange => remote_function( > :url => { :action => > "get_plan_with_ajax", :id => @customer.id}, > :with => "Form.Element.serialize(this)", > :method => "''post''", > :loading => > ''$(\''mem_ajax_status\'').innerHTML = \''loading ...\'''', > :complete => > ''eval(request.responseText);$(\''mem_ajax_status\'').innerHTML = \'' \'''' > )}) %> >Thanks for the help, I''ve played around with it today and got this code: <%= collection_select(''rig'', ''person_id'', @people, ''id'', ''name'', {:include_blank => true}, {:onchange => remote_function( :url => { :controller=>"person", :action => "get_with_ajax" }, :with => "this.value", :method => "''get''", :loading => ''$(\''mem_ajax_status\'').innerHTML = \''loading ...\'''', :complete => eval(request.responseText);$(\''mem_ajax_status\'').innerHTML = \'' \'''' )}) %> Which is sending a request to the server as follows: GET /person/get_with_ajax?3&_= HTTP/1.1" 500 5339 - -> /person/get_with_ajax?3&_ I''m assuming I want my url to look like /person/get_with_ajax/3 in order to access my ''get_with_ajax'' action in the person controller (for person_id=3, etc. I''m then stuck on exactly where the reply from the server goes (I don''t really understand what is happening on the :loading and :complete events.) I''d essentially like to dump this as text on the page, not in a textbox/textarea. Thanks, Mark
Mark Paxton wrote:> David Morton wrote: > >> Mark Paxton wrote: > > [snip] > >> I had trouble getting this to work. Here''s a snippet of code I used for >> the select box. I use the eval to evaluate the code sent back from the >> server to update the various fields. I''m not sure if I can explain it >> at the moment... It might rely on some patches that I have been >> submitting for send_ajax and such. >> >> <%= collection_select(''plan'', ''name'', @plans, ''id'', ''name'', >> {:include_blank => true}, >> {:onchange => remote_function( >> :url => { :action => >> "get_plan_with_ajax", :id => @customer.id}, >> :with => "Form.Element.serialize(this)", >> :method => "''post''", >> :loading => >> ''$(\''mem_ajax_status\'').innerHTML = \''loading ...\'''', >> :complete => >> ''eval(request.responseText);$(\''mem_ajax_status\'').innerHTML = >> \'' \'''' >> )}) %> >> > Thanks for the help, I''ve played around with it today and got this code: > > <%= collection_select(''rig'', ''person_id'', @people, ''id'', ''name'', > {:include_blank => true}, > {:onchange => remote_function( > :url => { :controller=>"person", :action => "get_with_ajax" }, > :with => "this.value", > :method => "''get''", > :loading => ''$(\''mem_ajax_status\'').innerHTML = \''loading ...\'''', > :complete => > eval(request.responseText);$(\''mem_ajax_status\'').innerHTML = \'' \'''' > )}) %> > > Which is sending a request to the server as follows: > > GET /person/get_with_ajax?3&_= HTTP/1.1" 500 5339 > - -> /person/get_with_ajax?3&_> > I''m assuming I want my url to look like /person/get_with_ajax/3 in order > to access my ''get_with_ajax'' action in the person controller (for > person_id=3, etc. > > I''m then stuck on exactly where the reply from the server goes (I don''t > really understand what is happening on the :loading and :complete > events.) I''d essentially like to dump this as text on the page, not in > a textbox/textarea. >Well looks like I''ve sorted it: <%= collection_select(''rig'', ''person_id'', @people, ''id'', ''name'', {:include_blank => true} ) %> <%= observe_field (''rig_person_id'', :frequency => 1, :update => ''person_details'', :url => { :controller => ''person'', :action=> ''get_with_ajax'' }, :with => "''id='' + escape(value)" ) %><br /> <div id=''person_details''> </div> With the help of this very useful how-to: http://wiki.rubyonrails.com/rails/show/How+to+make+a+real-time+search+box+with+the+Ajax+helpers Cheers, Mark
I''ve been concerned about the overhead of running multiple observe_field scripts - I have a table-view, with at least two fields per row which need to trigger an ajax update. In the end I used a render_partial for each row to generate a script fragment: function ajax_update_<%= id_to_update %>() { new Ajax.Updater(''<%= id_to_update %>'', ''/project/update_thing'', {onLoading:function(request){thing.reset()}, onComplete:function(request){new Effect.Highlight(''<%= id_to_update %>'');}, parameters:Form.serialize(''<%= form_to_serialize %>''), asynchronous:true}); } Which works perfectly-ish. For some reason when updating a table row neither firefox nor safari want to properly space the newly added column - but that''s a problem for another time... sam On 5/1/05, Mark Paxton <mark-LgYV4FD66f9DpWgnxyDo8Q@public.gmane.org> wrote:> Mark Paxton wrote: > > David Morton wrote: > > > >> Mark Paxton wrote: > > > > [snip] > > > >> I had trouble getting this to work. Here''s a snippet of code I used for > >> the select box. I use the eval to evaluate the code sent back from the > >> server to update the various fields. I''m not sure if I can explain it > >> at the moment... It might rely on some patches that I have been > >> submitting for send_ajax and such. > >> > >> <%= collection_select(''plan'', ''name'', @plans, ''id'', ''name'', > >> {:include_blank => true}, > >> {:onchange => remote_function( > >> :url => { :action => > >> "get_plan_with_ajax", :id => @customer.id}, > >> :with => "Form.Element.serialize(this)", > >> :method => "''post''", > >> :loading => > >> ''$(\''mem_ajax_status\'').innerHTML = \''loading ...\'''', > >> :complete => > >> ''eval(request.responseText);$(\''mem_ajax_status\'').innerHTML > >> \'' \'''' > >> )}) %> > >> > > Thanks for the help, I''ve played around with it today and got this code: > > > > <%= collection_select(''rig'', ''person_id'', @people, ''id'', ''name'', > > {:include_blank => true}, > > {:onchange => remote_function( > > :url => { :controller=>"person", :action => "get_with_ajax" }, > > :with => "this.value", > > :method => "''get''", > > :loading => ''$(\''mem_ajax_status\'').innerHTML = \''loading ...\'''', > > :complete => > > eval(request.responseText);$(\''mem_ajax_status\'').innerHTML = \'' \'''' > > )}) %> > > > > Which is sending a request to the server as follows: > > > > GET /person/get_with_ajax?3&_= HTTP/1.1" 500 5339 > > - -> /person/get_with_ajax?3&_> > > > I''m assuming I want my url to look like /person/get_with_ajax/3 in order > > to access my ''get_with_ajax'' action in the person controller (for > > person_id=3, etc. > > > > I''m then stuck on exactly where the reply from the server goes (I don''t > > really understand what is happening on the :loading and :complete > > events.) I''d essentially like to dump this as text on the page, not in > > a textbox/textarea. > > > Well looks like I''ve sorted it: > > <%= collection_select(''rig'', ''person_id'', @people, ''id'', ''name'', > {:include_blank => true} ) %> > <%= observe_field (''rig_person_id'', > :frequency => 1, > :update => ''person_details'', > :url => { :controller => ''person'', :action=> ''get_with_ajax'' }, > :with => "''id='' + escape(value)" ) %><br /> > <div id=''person_details''> </div> > > With the help of this very useful how-to: > http://wiki.rubyonrails.com/rails/show/How+to+make+a+real-time+search+box+with+the+Ajax+helpers > > Cheers, > Mark > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- sam http://www.magpiebrain.com/
Mark Paxton <mark@...> writes:> > > > > [snip]> > > Well looks like I''ve sorted it: > > <%= collection_select(''rig'', ''person_id'', <at> people, ''id'', ''name'', > {:include_blank => true} ) %> > <%= observe_field (''rig_person_id'', > :frequency => 1, > :update => ''person_details'', > :url => { :controller => ''person'', :action=> ''get_with_ajax'' }, > :with => "''id='' + escape(value)" ) %><br /> > <div id=''person_details''> </div> > > With the help of this very useful how-to: >http://wiki.rubyonrails.com/rails/show/How+to+make+a+real-time+search+box+with+the+Ajax+helpers> > Cheers, > Mark >Thanks for this code, it''s what I was looking for. I have it working for me, but with a problem. The action I''m calling is an .rhtml file that simply has an img tag. I only want the image to appear in the div. Unfortunately, the entire page is getting duplicated there. I''ve tried using :partial => and :layout => false, but it alway renders the whole page with all me css, header, etc. Any ideas? thanks, Matt