[Rails] AJAX - Having Multiple SELECT (Drop Down), and dependency

Ramanan
2006-08-17T18:25:51Z

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:




   <% @states.each do |state| %>
      <option value="<%= state.id %>">
      <%= state.state_name %>

   <% end %>




	 <select id="city_id" name="city_id"
disabled="disabled">
Select City


    <%= observe_field("state[id]",
        :update => "city_container",
        :url => { :action => :select_city },
        :with => "''id=''+value",
        :on => "changed")
%>



         <select id="locality_id"
name="home_for_rent[locality_id]"
                       disabled="disabled">
Select Locality


     <%= observe_field("city_id",
         :update => "locality_container",
         :url => { :action => :select_locality }
        :with => "''id=''+value",
        :on => "changed")
     %>



Controller Code

  def select_city
       @cities = City.find_all_by_state_id(@params["id"])
       @html = "<select id=''city_id''
name=''locality[city_id]''>"
@html += " Select
City "
       @cities.each do |@city|
           @html += "<option
value=''#{@city.id}''>#{@city.city_name} "
       end
@html += " "
      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 += " Select
Locality "
        @localities.each do |@locality|
            @html += "<option
value=''#{@locality.id}''>#
{@locality.locality_name} "
        end
@html += " "

#      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

PS : I have tried with the Cascading SELECT using AJAX but it did not
work with both IE and FF.  Did anyone encounter this kind of situation?


--
Posted via http://www.ruby-forum.com/.