# about dynamic select

**URL:** <https://discuss.rubyonrails.org/t/about-dynamic-select/32105>\
**Category:** rubyonrails-talk\
**Created:** [July 20, 2008, 1:59pm UTC](https://discuss.rubyonrails.org/t/about-dynamic-select/32105 "2008-07-20T13:59:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 20, 2008, 1:59pm UTC](https://discuss.rubyonrails.org/t/about-dynamic-select/32105/1 "2008-07-20T13:59:48Z")

</div>

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 &nbsp;&nbsp;&nbsp;&nbsp;@user=User.new(params[:user]) &nbsp;&nbsp;&nbsp;&nbsp;if params[:user]&&@user.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:login]=@user.id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:register]="#{@user.name}已创建" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action=\>"upanel" &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :action=\>"register" &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end def get\_region\_kind &nbsp;&nbsp;&nbsp;&nbsp;@user=User.new &nbsp;&nbsp;&nbsp;&nbsp;if params[:region\_kind]=="省" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render(:template=\>"login/province") &nbsp;&nbsp;&nbsp;&nbsp;elsif params[:region\_kind]=="直辖市" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render(:template=\>"login/direct\_city") &nbsp;&nbsp;&nbsp;&nbsp;elsif params[:region\_kind]=="特别行政区" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render(:template=\>"login/special\_area") &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end &nbsp;&nbsp;def get\_capital &nbsp;&nbsp;&nbsp;&nbsp;@user=User.new &nbsp;&nbsp;&nbsp;&nbsp;capital\_select=params[:capital\_select] &nbsp;&nbsp;&nbsp;&nbsp;case capital\_select &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when "石家庄" then @section=User::SHIJIAZHUANG &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when "秦皇岛" then @section=User::QINGHUANGDAO &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end 2 view: register.rhtml \<%form\_for :user,:url=\>{:action=\>"register"},:html=\>{:multipart=\>true} do |f|-%\> &nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<label\>照片\</label\> &nbsp;&nbsp;&nbsp;&nbsp;\<%=f.file\_field "file"-%\>图片名称使用中文以外字符 &nbsp;&nbsp;\</p\> \<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<label\>行政区划类别\</label\> &nbsp;&nbsp;&nbsp;&nbsp;\<%=select\_tag "regionalism",options\_for\_select(["请选择","省","直辖市","特别行政区"])-%\> &nbsp;&nbsp;\</p\> \<%=observe\_field "regionalism",:update=\>"region",:url=\>{:action=\>"get\_region\_kind"},:with=\>"region\_kind"-%\> &nbsp;&nbsp;\<div id="region"\> &nbsp;&nbsp;\</div\> \<%=submit\_tag "提交",:disable\_with=\>"请稍候..."-%\> \<%end-%\>

province.rhtml \<p\> &nbsp;&nbsp;\<label\>省\</label\> &nbsp;&nbsp;\<%=select "user","province",User::PROVINCE-%\> \</p\> \<p\> &nbsp;&nbsp;\<label\>市\</label\> &nbsp;&nbsp;\<%=select "user","city",User::CAPITAL-%\> \</p\> \<%=observe\_field "user\_city",:update=\>"user\_section",:url=\>{:action=\>"get\_capital"},:with=\>"capital\_select"-%\> \<p\> &nbsp;&nbsp;\<label\>区\</label\> &nbsp;&nbsp;\<%=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.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [July 20, 2008, 2:20pm UTC](https://discuss.rubyonrails.org/t/about-dynamic-select/32105/2 "2008-07-20T14:20:41Z")

</div>

Think about what's going to happen here: the inside of user\_section will get replaced by what's rendered. So you'll end up with \<select id="user\_section" ...\> &nbsp;&nbsp;\<select id="user\_section" ...\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;\</select\> \</select\>

which doesn't look good.

Fred
