I want to design a dynamic select container.That is :when a user selects a province,there is a city select container dropping out.Then when a user select a city,there is a city section container dropping out.I use the observe_field to manage these. 1 controller: def register @user=User.new(params[:user]) if params[:user]&&@user.save session[:login]=@user.id flash[:register]="#{@user.name}已创建" redirect_to :action=>"upanel" else render :action=>"register" end end def get_region_kind @user=User.new if params[:region_kind]=="省" render(:template=>"login/province") elsif params[:region_kind]=="直辖市" render(:template=>"login/direct_city") elsif params[:region_kind]=="特别行政区" render(:template=>"login/special_area") end end def get_capital @user=User.new capital_select=params[:capital_select] case capital_select when "石家庄" then @section=User::SHIJIAZHUANG when "秦皇岛" then @section=User::QINGHUANGDAO ... end end 2 view: register.rhtml <%form_for :user,:url=>{:action=>"register"},:html=>{:multipart=>true} do |f|-%> <p> <label>照片</label> <%=f.file_field "file"-%>图片名称使用中文以外字符 </p> <p> <label>行政区划类别</label> <%=select_tag "regionalism",options_for_select(["请选择","省","直辖市","特别行政区"])-%> </p> <%=observe_field "regionalism",:update=>"region",:url=>{:action=>"get_region_kind"},:with=>"region_kind"-%> <div id="region"> </div> <%=submit_tag "提交",:disable_with=>"请稍候..."-%> <%end-%>
province.rhtml <p> <label>省</label> <%=select "user","province",User::PROVINCE-%> </p> <p> <label>市</label> <%=select "user","city",User::CAPITAL-%> </p> <%=observe_field "user_city",:update=>"user_section",:url=>{:action=>"get_capital"},:with=>"capital_select"-%> <p> <label>区</label> <%=select "user","section",User::SHIJIAZHUANG-%> </p>
get_capital.rhtml <%=select "user","section",@section-%>
Now,when i select province,the codes work.But when select the city,there is blank city section select container.It is blank and not what i want.I think the model should be right.so where is wrong? We can not use the several user objects in the register.rhtml file to register an user? Or there is another way to manage my question.