I have a form with a select box in it. I need some secondary information based on the the select box, option 1 nothing, option 2: 2 fields, option 3: 1 field. I''m thinking this is where AJAX can make me look good. After the user selects an option an AJAX call sends the selection back to the server which sends the appropriate form fragment back and the user continues. Just to make life more interesting the select box is populated with data from a db table and the secondary field names are in that table. I think that this can all be handled by the controller but thought I would mention it just in case. My problem is that I am learning RoR and AJAX at the same time and I can''t seem to find the bits I need. How do I get a message back to the server when the selection in a selectbox is changed. Is there a Rails helper for this or do I have to dig up some javascript code to do this? - Michael
I believe you need to ''observe'' the field. http://api.rubyonrails.com/classes/ActionView/Helpers/JavascriptHelper.html#M000369 On 7/5/05, Michael King <kingmt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a form with a select box in it. I need some secondary > information based on the the select box, option 1 nothing, option 2: 2 > fields, option 3: 1 field. I''m thinking this is where AJAX can make me > look good. After the user selects an option an AJAX call sends the > selection back to the server which sends the appropriate form fragment > back and the user continues. > > Just to make life more interesting the select box is populated with > data from a db table and the secondary field names are in that table. > I think that this can all be handled by the controller but thought I > would mention it just in case. > > My problem is that I am learning RoR and AJAX at the same time and I > can''t seem to find the bits I need. How do I get a message back to the > server when the selection in a selectbox is changed. Is there a Rails > helper for this or do I have to dig up some javascript code to do > this? > > - Michael > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Michael King wrote:><snip> > >My problem is that I am learning RoR and AJAX at the same time and I >can''t seem to find the bits I need. How do I get a message back to the >server when the selection in a selectbox is changed. Is there a Rails >helper for this or do I have to dig up some javascript code to do >this? ></snip> >Michael, New at this myself (no guarantees that its the right or rails way) but I use something like this for a similar situation: View: <label for="type">Type:</label> <%= select("object", "type", Object.Types, {}, {:onchange=>onChange="new Ajax.Updater(''_selector'', ''/object/ajax_type/'' + this.value, {asynchronous:true})"}) %> <div id="_selector"> </div> Controller: def ajax_type render_partial(@params[:id]) end HTH -- Mike Gilbert email : mike-jLbnyU+aYbUAvxtiuMwx3w@public.gmane.org Technical Director fax : +353 68 470 01 XL CRS mobile: +353 87 677 2055 Lisselton, Listowel phone : +353 68 470 10 Co. Kerry, Ireland
Hi Michael, I''ve written a previous blog post on select drop-downs and Ajax that may be of use. http://www.roryhansen.ca/?p=9 Rory -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Michael King Sent: Tuesday, July 05, 2005 10:25 AM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] triggering AJAX off selection I have a form with a select box in it. I need some secondary information based on the the select box, option 1 nothing, option 2: 2 fields, option 3: 1 field. I''m thinking this is where AJAX can make me look good. After the user selects an option an AJAX call sends the selection back to the server which sends the appropriate form fragment back and the user continues. Just to make life more interesting the select box is populated with data from a db table and the secondary field names are in that table. I think that this can all be handled by the controller but thought I would mention it just in case. My problem is that I am learning RoR and AJAX at the same time and I can''t seem to find the bits I need. How do I get a message back to the server when the selection in a selectbox is changed. Is there a Rails helper for this or do I have to dig up some javascript code to do this? - Michael _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 5-jul-2005, at 19:40, Rory Hansen wrote:> Hi Michael, > > I''ve written a previous blog post on select drop-downs and Ajax > that may be > of use. > > http://www.roryhansen.ca/?p=9 >If your dropdowns reside in a separate form, then you can do what I did - observe the whole form and then fetch the renewed form contents with all the 3 dropdowns, with proper options already filtered and selected, and just insert it into the same form tag. -- Julian "Julik" Tarkhanov