Ramanan
2006-Aug-17 18:25 UTC
[Rails] AJAX - Having Multiple SELECT (Drop Down), and dependency
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
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/.
