I have the standard Country - State - City select boxes in a form and want the select options to cascade dynamically. There are a few hits for similar setups in the forum but I need something more fundamental. Is there a website / book which details how to build this from scratch? AWDwR v2 uses a search example which hasn''t helped me much... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Taylor, I don''t know of a good tutorial on this but we just completed a thread on this very thing in past couple of days with an example. Be sure to read the whole thing though because we ended up having to wrap the Select tag in a Div to make it work on IE. See http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/b7570e72171a37e9/# -Paul --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Paul, Thanks! I found that thread late last night and worked into the wee hours of the morning getting it working. It was pivotal to my success. The only difference in my usage was I had to use page.replace_html because I had two levels of dependency and render :partial can only update one item. Thanks again for that tutorial! Here is how I did it for the archives: ---------VIEWS----------- ******_form:********* <%= render(:partial => ''country_options'', :layout => false) %> <%= render(:partial => ''state_options'', :layout => false) %> <%= render(:partial => ''city_options'', :layout => false) %> ********_country_options:********* <p> <label for="building_country">Country</label> <select name="building[country]" id="building[country]"> <option value="0"> -- </option> <%= options_from_collection_for_select(@countries, :id, :name) %> <%= observe_field "building[country]", :url => {:controller => "buildings", :action => "country_changed"}, :with => "building[country]", :update => "building[state]" %> </select> </p> *****_state_options:******* <div id="state_options"> <p> <label for="building_state">State / Region</label> <select name="building[state]" id="building[state]"> <option value="0"> -- </option> <%= options_from_collection_for_select(@states, :id, :name) %> <%= observe_field "building[state]", :url => {:controller => "buildings", :action => "state_changed"}, :with => "building[state]", :update => "building[city]" %> </select> </p> </div> *******_city_options:****** <div id="city_options"> <p> <label for="building_city">City</label> <select name="building[city]" id="building[city]"> <option value=""> -- </option> <%= options_from_collection_for_select(@cities, :id, :name) %> </select> </p> </div> -----CONTROLLER-------- def create if session[:person_id] unless params[:building] @building = Building.new @countries = Country.find(:all) @states = [] @cities = [] else @building = Building.new(params[:building]) @building.save flash[:notice] = ''Building was successfully created.'' redirect_to :action => ''list'' end else redirect_to :controller => ''login'', :action => ''login'' end end def country_changed # avoid freaking out if user deselects country if params[:building][:country] == "0" @states = [] else @states = Country.find(params[:building][:country]).states end @cities = [] # update state list and reset city list render :update do |page| page.replace_html ''state_options'', :partial => ''state_options'' page.replace_html ''city_options'', :partial => ''city_options'' end end def state_changed # avoid freaking out if user deselects state if params[:building][:state] == "0" @cities = [] else @cities = State.find(params[:building][:state]).cities end # update city list render :update do |page| page.replace_html ''city_options'', :partial => ''city_options'' end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Taylor, Maybe you have already seen this, but there is a plugin that creates "conditional" selects based on your models (http://www.agilewebdevelopment.com/plugins/related_select_forms) Take note of the second comment on the above page, if you are on edge rails, you will have to do as the comment issuers states (at least I had to). The plugin worked really well for me. -Fredrik On Dec 29, 6:55 pm, Taylor Strait <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Paul, > > Thanks! I found that thread late last night and worked into the wee > hours of the morning getting it working. It was pivotal to my success. > The only difference in my usage was I had to use page.replace_html > because I had two levels of dependency and render :partial can only > update one item. Thanks again for that tutorial! Here is how I did it > for the archives: > > ---------VIEWS----------- > ******_form:********* > > <%= render(:partial => ''country_options'', :layout => false) %> > > <%= render(:partial => ''state_options'', :layout => false) %> > > <%= render(:partial => ''city_options'', :layout => false) %> > > ********_country_options:********* > <p> > <label for="building_country">Country</label> > <select name="building[country]" id="building[country]"> > <option value="0"> -- </option> > <%= options_from_collection_for_select(@countries, :id, :name) > %> > <%= observe_field "building[country]", > :url => {:controller => "buildings", :action => > "country_changed"}, > :with => "building[country]", > :update => "building[state]" %> > </select> > </p> > > *****_state_options:******* > <div id="state_options"> > <p> > <label for="building_state">State / Region</label> > <select name="building[state]" id="building[state]"> > <option value="0"> -- </option> > <%= options_from_collection_for_select(@states, :id, :name) %> > <%= observe_field "building[state]", > :url => {:controller => "buildings", :action => > "state_changed"}, > :with => "building[state]", > :update => "building[city]" %> > </select> > </p> > </div> > > *******_city_options:****** > <div id="city_options"> > <p> > <label for="building_city">City</label> > <select name="building[city]" id="building[city]"> > <option value=""> -- </option> > <%= options_from_collection_for_select(@cities, :id, :name) %> > </select> > </p> > </div> > > -----CONTROLLER-------- > def create > if session[:person_id] > unless params[:building] > @building = Building.new > @countries = Country.find(:all) > @states = [] > @cities = [] > else > @building = Building.new(params[:building]) > @building.save > flash[:notice] = ''Building was successfully created.'' > redirect_to :action => ''list'' > end > else > redirect_to :controller => ''login'', :action => ''login'' > end > end > > def country_changed > # avoid freaking out if user deselects country > if params[:building][:country] == "0" > @states = [] > else > @states = Country.find(params[:building][:country]).states > end > @cities = [] > > # update state list and reset city list > render :update do |page| > page.replace_html ''state_options'', :partial => ''state_options'' > page.replace_html ''city_options'', :partial => ''city_options'' > end > end > > def state_changed > # avoid freaking out if user deselects state > if params[:building][:state] == "0" > @cities = [] > else > @cities = State.find(params[:building][:state]).cities > end > > # update city list > render :update do |page| > page.replace_html ''city_options'', :partial => ''city_options'' > end > end > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Fri, 2006-12-29 at 18:47 +0000, fredrikbach wrote:> Hi Taylor, > > Maybe you have already seen this, but there is a plugin that creates > "conditional" selects based on your models > (http://www.agilewebdevelopment.com/plugins/related_select_forms) > > Take note of the second comment on the above page, if you are on edge > rails, you will have to do as the comment issuers states (at least I > had to). > > The plugin worked really well for me. >---- thanks for the link...it worked well for me too. Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---