CityHelper是一个提供国家,省,市选择框的小插件,这次更新增加了国家和国际化支持,目前只支持简体中文和英语。
安装
$ script/plugin install git://github.com/yzhang/city_helper.git
这会在你的config/locales目录下安装一个cities目录,这个目录包含了i18n的信息,还会在config/initializers目录增加一个city_helper.rb来设置i18n的加载目录。
使用
country_select(:user, :country, {
:only => [:china], # 指定要显示的国家
:update => 'state_select_id', # 指定国家改变后要更新的省份选择框的id
}, html_options)
state_select(:user, :state, {
:country => :china, # 指定默认显示那个国家的省份
:countries => [:china], # 只有在country_select中设置了update属性时才需设置这个属性
# 这个属性应该与country_select中的:only属性保持一致
:update => 'city_select_id', # 指定省份变动时要更新的城市选择框ID
}, html_options)
city_select(:user, :city, {
:country => :china, # 设置默认的城市列表所在国家
:state => :beijing, # 设置默认的城市列表所在省份
}, html_options)
下面是一个国家,省,市联动的使用范例:
country_select(:user, :country,
:only => [:china, :japan, :united_states],
:update => 'state')
state_select(:user, :state,
:country => :china,
:countries => [:china, :japan, :united_states],
:update => 'city')
city_select_tag(:user, :city, :country => :china, :state => :beijing)
你也可以通过Formbuilder或者xxx_tag使用这些helper:
form.country_select :country
form.state_select :state
form.city_select :city
country_select_tag :country
state_select_tag :state
city_select_tag :city
如果需要增加更多城市,可以通过修改插件中的city_helper中的常量来实现。
最后,最近Blog迁移,不小心把另一个Blog的内容混进了RSS,给大家造成困扰,实在抱歉。

