Jason Fox
2006-Aug-16 15:20 UTC
[Rails] Multiple (AJAX) Observers on the Same Field and MSIE
I have been using multiple observers, i.e., observe_field(), on the same input field and relying on them to execute in the same order that they appear in the page. This has been working fine in FireFox, but it does not seem to work in MSIE; the requests come in and are processed in a different order. Now, I''ve always been a little hesitant about using this technique, but it always seemed to work. My question is, is there a better way to do this? -- Posted via http://www.ruby-forum.com/.
Bill Walton
2006-Aug-16 16:43 UTC
[Rails] Multiple (AJAX) Observers on the Same Field and MSIE
Hi Jason, Jason Fox wrote:>I have been using multiple observers, i.e., observe_field(), > on the same input field and relying on them to execute in > the same order that they appear in the page.You''ve got me curious... Why multiple observers on the same field? I assume you''re observing for the same event given your "relying on them to execute..." statement. If so, then the question would be "why not put all the code in one place in the order you need it to execute?" Best regards, Bill
Jason Fox
2006-Aug-16 17:11 UTC
[Rails] Re: Multiple (AJAX) Observers on the Same Field and MSIE
Bill Walton wrote:> You''ve got me curious... Why multiple observers on the same field? I > assume you''re observing for the same event given your "relying on them > to execute..." statement. If so, then the question would be "why not put > all the code in one place in the order you need it to execute?"I am using the multiple observers so that I can update multiple sections of the page when the input field changes, e.g., user checks a checkbox and both DIV1 and DIV2 are updated, but the update logic for DIV2 depends on changes performed in the action that handles updating DIV1. The multiple observer method was a bit messy but always seemed to work (except in MSIE). -- However, I think I''ve figured out a better way to do it after doing some more digging. There is an update_element_function Prototype helper that I can embed in the partial returned by code that updates DIV1, that, when reached, will execute a second AJAX call to update DIV2. I think this will reliably "chain" my updates together in a way that will work in all browsers. Thoughts? Thanks for the response! Regards, Jason -- Posted via http://www.ruby-forum.com/.
Bill Walton
2006-Aug-16 17:58 UTC
[Rails] Re: Multiple (AJAX) Observers on the Same Field and MSIE
Hi Jason, Jason Fox wrote:> > I am using the multiple observers so that I can > update multiple sections of the page when the > input field changes> Thoughts?I''m thinking you''re using the :update option on observe_field. If that''s right, I''m thinking ... Don''t. Use RJS (pick up Cody Fauser''s $10 PDF book(let) on O''Reilly). Then you''ll only need one observer and you can update the page to your heart''s content from the RJS template. Or maybe I''m missing something about what you''re trying to accomplish. In which case, I apologize for my density ;-) Best regards, Bill
Ramanan
2006-Aug-16 19:00 UTC
[Rails] Re: Multiple (AJAX) Observers on the Same Field and MSIE
Hi I have three drop downs (State, City, Locality) and would like to populate City based on State selection and Locality based on City selection. I have observers on two fields (State, and City). The observer_field on state works fine and it populates city based on state selection. But the observer on city doesn''t populate Locality. Now sure whats wrong with my code. here is the code snippet RHTML Code: <tr valign="top" align="left"> <td valign="top" align="left"> <select id="state[id]" name="state[id]"> <% @states.each do |state| %> <option value="<%= state.id %>"> <%= state.state_name %> </option> <% end %> </select> </td> <td valign="top" align="left"> <div id="city_container"> <select id="city_id" name="city_id" disabled="disabled"> <option value="">Select City</option> </select> </div> <%= observe_field("state[id]", :update => "city_container", :url => { :action => :select_city }, :with => "''id=''+value", :on => "changed") %> </td> <td valign="top" align="left"> <div id="locality_container"> <select id="locality_id" name="home_for_rent[locality_id]" disabled="disabled"> <option value="">Select Locality</option> </select> </div> <%= observe_field("city_id", :update => "locality_container", :url => { :action => :select_locality } :with => "''id=''+value", :on => "changed") %> </td> Controller Code def select_city @cities = City.find_all_by_state_id(@params["id"]) @html = "<select id=''city_id'' name=''locality[city_id]''>" @html += "<option value="">Select City</option>" @cities.each do |@city| @html += "<option value=''#{@city.id}''>#{@city.city_name}</option>" end @html += "</select>" render_text @html end def select_locality @localities = Locality.find_all_by_city_id(@params["id"]) @html = "<select id=''locality_id'' name=''home_for_rent[locality_id]''>" @html += "<option value="">Select Locality</option>" @localities.each do |@locality| @html += "<option value=''#{@locality.id}''># {@locality.locality_name}</option>" end @html += "</select>" # render_text "test" render_text @html end I tried with render_text "test" just to make sure that i get the text "test" back on selecting City. But that also doesn''t work. Is there a work around? Thx for your help, Ramanan
Gerben van de Wiel
2006-Aug-16 19:53 UTC
[Rails] Re: Multiple (AJAX) Observers on the Same Field and MSIE
I think this link will solve the problem. and it is nice coding style too ;). http://rails.techno-weenie.net/tip/2006/6/20/cascading_select_boxes_with_rjs_even_on_ie Yours, Retonator On 8/16/06, Ramanan <nramanan@hotmail.com> wrote:> > Hi > > I have three drop downs (State, City, Locality) and would like to populate > City based on State selection and Locality based on City selection. > > I have observers on two fields (State, and City). The observer_field on > state > works fine and it populates city based on state selection. But the > observer > on city doesn''t populate Locality. Now sure whats wrong with my code. > > here is the code snippet > > RHTML Code: > > <tr valign="top" align="left"> > <td valign="top" align="left"> > <select id="state[id]" name="state[id]"> > <% @states.each do |state| %> > <option value="<%= state.id %>"> > <%= state.state_name %> > </option> > <% end %> > </select> > </td> > <td valign="top" align="left"> > <div id="city_container"> > <select id="city_id" name="city_id" disabled="disabled"> > <option value="">Select City</option> > </select> > </div> > <%= observe_field("state[id]", > :update => "city_container", > :url => { :action => :select_city }, > :with => "''id=''+value", > :on => "changed") > %> > </td> > <td valign="top" align="left"> > <div id="locality_container"> > <select id="locality_id" name="home_for_rent[locality_id]" > disabled="disabled"> > <option value="">Select Locality</option> > </select> > </div> > <%= observe_field("city_id", > :update => "locality_container", > :url => { :action => :select_locality } > :with => "''id=''+value", > :on => "changed") > %> > </td> > > > Controller Code > > def select_city > @cities = City.find_all_by_state_id(@params["id"]) > @html = "<select id=''city_id'' name=''locality[city_id]''>" > @html += "<option value="">Select City</option>" > @cities.each do |@city| > @html += "<option value=''#{@city.id}''>#{@city.city_name > }</option>" > end > @html += "</select>" > render_text @html > end > > def select_locality > @localities = Locality.find_all_by_city_id(@params["id"]) > @html = "<select id=''locality_id'' > name=''home_for_rent[locality_id]''>" > @html += "<option value="">Select Locality</option>" > @localities.each do |@locality| > @html += "<option value=''#{@locality.id}''># > {@locality.locality_name}</option>" > end > @html += "</select>" > > # render_text "test" > render_text @html > end > > > I tried with render_text "test" just to make sure that i get the text > "test" > back on selecting City. But that also doesn''t work. Is there a work > around? > > Thx for your help, > Ramanan > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060816/be1ba24b/attachment.html
Jason Fox
2006-Aug-16 21:09 UTC
[Rails] Re: Re: Multiple (AJAX) Observers on the Same Field and MSIE
Bill Walton wrote:> I''m thinking you''re using the :update option on observe_field. If > that''s right, I''m thinking ... Don''t. Use RJS (pick up Cody Fauser''s $10 PDF > book(let) on O''Reilly). Then you''ll only need one observer and you can > update the page to your heart''s content from the RJS template. Or maybe > I''m missing something about what you''re trying to accomplish. In which > case, I apologize for my density ;-)Yes, I was using the :update option on observe_field. -- I had played around with RJS templates before but could never get them to work for some reason. I took your advice, though, and purchased the PDF from O''Reilly on RJS templates. After a quick read-through I was able to consolidate my code and really clean things up nicely! Whatever problems I had in the past with RJS templates are now gone. I think it might have had something to do with me not knowing to run the rake rails:update script. RJS templates are a very nice addition to the Rails framework. Anyways, thanks for the pointer! It really helped! Regards, Jason -- Posted via http://www.ruby-forum.com/.
Bill Walton
2006-Aug-16 23:28 UTC
[Rails] Re: Re: Multiple (AJAX) Observers on the Same Field and MSIE
Jason Fox wrote:> Anyways, thanks for the pointer! It really helped!You''re welcome. And congrats !!! Best regards, Bill
Ramanan
2006-Aug-16 23:56 UTC
[Rails] Re: Re: Multiple (AJAX) Observers on the Same Field and MSIE
Gerben van de Wiel wrote:> I think this link will solve the problem. > and it is nice coding style too ;). > > http://rails.techno-weenie.net/tip/2006/6/20/cascading_select_boxes_with_rjs_even_on_ie > > Yours, > RetonatorI could not make it to work. May be I am doing something wrong. The comments for application_helper.rb says that "It assumes that the function update_select_options is already in the open document". What does this mean? Do I need to explicitly include "update_select_options" code in my document or is this code is generated by application_helper.rb. Can somebody tell me how to include "update_select_options" in my document. Thx. Ramanan -- Posted via http://www.ruby-forum.com/.