city_helper: 省市二级联动Select插件

此插件已更新,请移步这里: http://www.letrails.cn/archives/city-helper-update-i18n-and-country-support/

city_helper提供了几个简单的helper来实现省及城市的二级联动select,目前只支持国内城市,经过测试的浏览器:FF3, Chrome 2 beta, IE7,
Safari 4。

安装


$ script/plugin install git://github.com/yzhang/city_helper.git

使用
安装之后,你就可以在View中直接使用以下Helper了:


state_select :user, :state # 选择省份
capital_select :user, :city # 选择省会城市
city_select :user, :city # 选择所有地级及地级以上城市
state_and_city_select :user, :state, :city # 省市二级联动select

当然,你也可以在FormBuilder中使用,同时还有一个不需要object的_tag版本:


form.city_select :city
city_select_tag :city

你也可以传递options给city_helper,参数定义同select:


city_select :user, :city, options, html_options
state_and_city_select :user, :state, :city, {:city => city_options, :state => state_options}, {:city => city_html_options, :state => state_html_options}

state_and_city_select稍微复杂一点,下面给个例子:


state_and_city_select :user, :state, :city, {:state => {:include_blank => '请选择省'}, :city => {:include_blank => '请选择城市'}}

TODO

  1. 支持其它国家城市及国际化
  2. 和country_select集成,实现三级联动
This entry was posted in 插件. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • http://railser.cn 里克

    太好了,实验一下。谢谢

  • http://railser.cn 里克

    有2个小错误
    1、CITIES,keys中广西应为广东
    2、海南的城市没有加
    ‘海南’ => ['海口', '三亚'],

  • http://railser.cn/index.php/blog/sns-notes-china-province-and-city-select-plugin sns网站开发经验总结:中国省份城市插件(一) at 里克的自习室 – 关注Ruby和Rails学习与开发的博客

    [...] 网站:http://www.letrails.cn/archives/city-helper-a-plugin-provide-state-and-city-select [...]

  • lawrence

    不支持Ruby 1.9
    请在city_helper.rb第一行增加:
    # -*- coding: utf-8 -*-

无觅相关文章插件,快速提升流量