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][email protected]
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|-%>
照片 <%=f.file_field "file"-%>图片åç§°ä½¿ç”¨ä¸æ–‡ä»¥å¤–å—符
行政区划类别 <%=select_tag "regionalism",options_for_select(["请选择","çœ","直辖市","特别行政区"])-%>
<%=observe_field "regionalism",:update=>"region",:url=>{:action=>"get_region_kind"},:with=>"region_kind"-%>province.rhtml
çœ <%=select "user","province",User::PROVINCE-%>
市 <%=select "user","city",User::CAPITAL-%>
<%=observe_field "user_city",:update=>"user_section",:url=>{:action=>"get_capital"},:with=>"capital_select"-%>区 <%=select "user","section",User::SHIJIAZHUANG-%>
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.