Plugin: state_select, generate drop down selection box for states

September 12, 2006 at 2:34 pm 15 comments

I have wrote my first plugin(state_select). This plugin allows to create drop down list for states, same as country_select method in rails. I know this is not a big deal…

Curently it can generate state list for India, US, Canada, Australia and Spain(default is US).


  • state_select(object, method, country='US', options = {}, html_options = {})

Return select and option tags for the given object and method, using state_options_for_select to generate the list of option tags.

  • state_options_for_select(selected = nil, country = 'US')

Returns a string of option tags for states in a country. Supply a state name as selected to have it marked as the selected option tag.

NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.

The svn repository can be found at

Install plugin by

script/plugin install

Entry filed under: rails, rails plugins, Ruby, RubyonRails.

12 Balls Problem Lib file state_select.rb

15 Comments Add your own

  • 1. keith  |  September 14, 2006 at 3:41 pm

    Here is a patch to add Australian states:

    Index: lib/state_select.rb
    — lib/state_select.rb (revision 1)
    +++ lib/state_select.rb (working copy)
    @@ -23,6 +23,7 @@
    US_STATES=[“Alaska”, “Alabama”, “Arkansas”, “Arizona”, “California”, “Colorado”, “Connecticut”, “Washington D.C.”, “Delaware”, “Florida”, “Georgia”, “Hawaii”, “Iowa”, “Idaho”, “Illinois”, “Indiana”, “Kansas”, “Kentucky”, “Louisiana”, “Massachusetts”, “Maryland”, “Maine”, “Michigan”, “Minnesota”, “Missourri”, “Mississippi”, “Montana”, “North Carolina”, “North Dakota”, “Nebraska”, “New Hampshire”, “New Jersey”, “New Mexico”, “Nevada”, “New York”, “Ohio”, “Oklahoma”, “Oregon”, “Pennsylvania”, “Puerto Rico”, “Rhode Island”, “South Carolina”, “South Dakota”, “Tennessee”, “Texas”, “Utah”, “Virginia”, “Vermont”, “Washington”, “Wisconsin”, “West Virginia”, “Wyoming”] unless const_defined?(“US_STATES”)
    INDIA_STATES=[“Andhra Pradesh”, “Arunachal Pradesh”, “Assam”, “Bihar”, “Chhattisgarh”, “New Delhi”, “Goa”, “Gujarat”, “Haryana”, “Himachal Pradesh”, “Jammu and Kashmir”, “Jharkhand”, “Karnataka”, “Kerala”, “Madhya Pradesh”, “Maharashtra”, “Manipur”, “Meghalaya”, “Mizoram”, “Nagaland”, “Orissa””Punjab”, “Rajasthan”, “Sikkim”, “Tamil Nadu”, “Tripura”, “Uttaranchal”, “Uttar Pradesh”, “West Bengal”] unless const_defined?(“INDIA_STATES”)
    CANADA_STATES=[“Alberta”, “British Columbia”, “District of Columbia”, “Manitoba”, “New Brunswick”, “Newfoundland and Labrador”, “Northwest Territories”, “Nova Scotia”, “Nunavut”, “Prince Edward Island”, “Saskatchewan”, “Ontario”, “Quebec”] unless const_defined?(“CANADA_STATES”)
    + AUSTRALIA_STATES=[“Australian Capital Territory”, “New South Wales”, “Northern Territory”, “Queensland”, “South Australia”, “Tasmania”, “Victoria”, “Western Australia”] unless const_defined?(“AUSTRALIA_STATES”)

    class ActionView::Helpers::InstanceTag
    Index: README
    — README (revision 1)
    +++ README (working copy)
    @@ -6,7 +6,7 @@
    state_select(object, method, country=’INDIA’, options = {}, html_options = {})

    – Currently it supports only India, US and Canada
    + Currently it supports only India, US, Canada and Australia

    If you want to add other country plese send me the list of states and I will add them.

  • 2. Akhil Bansal  |  September 15, 2006 at 4:44 am

    Thanks Kaith,
    I have added it to the plugin itself.

  • 3. Vicent  |  October 4, 2006 at 7:09 pm

    ES_STATES=[ “Alava”, “Albacete”, “Alicante”, “Almeria”, “Asturias”, “Avila”, “Badajoz”, “Barcelona”, “Burgos”, “Caceres”, “Cadiz”, “Cantrabria”, “Castellón”, “Ceuta”, “Ciudad Real”, “Cordoba”, “Coruña, A”, “Cuenca”, “Girona”, “Granada”, “Guadalajara”, “Guipuzcoa”, “Huelva”, “Huesca”, “Islas Baleares”, “Jaen”,”León”, “Lleida”, “Lugo”, “Madrid”, “Malaga”, “Melilla”, “Murcia”, “Navarra”, “Ourense”, “Palencia”, “Palmas, Las”, “Pontevedra”, “Rioja, La”, “Salamanda”, “Santa Cruz de Tenerife”, “Segovia”, “Sevila”, “Soria”, “Tarragona”, “Teruel”, “Toledo”, “Valencia”, “Valladolid”, “Vizcaya”, “Zamora”, “Zaragoza”] unless const_defined?(“ES_STATES”)

  • 4. Akhil Bansal  |  October 5, 2006 at 5:38 am

    Thanks Vicent,
    I have added this to plugin itself.

  • 5. Ryan  |  October 10, 2006 at 11:48 pm

    Is there something available, or could you possibly write something to provide easy validation that verfies an actual entry was submitted? I would rather not write out all 50 states, and I think it’d be an improvment to the plugin.


  • 6. musica italiana  |  January 17, 2007 at 8:45 am

    musica italiana


  • 7. Joe  |  April 20, 2007 at 10:25 am

    UK COUNTIES, as per UK post code data

    SCOTTISH_STATES=[“Aberdeenshire”, “Angus”, “Argyll”, “Ayrshire”, “Banffshire”, “Caithness”, “Clackmannanshire”, “Dumfriesshire”, “Dunbartonshire”, “East Lothian”, “Fife”, “Inverness-Shire”, “Kincardineshire”, “Kinross-Shire”, “Kirkcudbrightshire”, “Lanarkshire”, “Midlothian”, “Morayshire”, “Nairnshire”, “Peeblesshire”, “Perthshire”, “Renfrewshire”, “Ross-Shire”, “Roxburghshire”, “Selkirkshire”, “Stirlingshire”, “Sutherland”, “West Lothian”, “Wigtownshire”] unless const_defined?(“SCOTTISH_STATES”)

    WELSH_STATES=[“Clwyd”, “Dyfed”, “Gwent”, “Gwynedd”, “Mid Glamorgan”, “Powys”, “South Glamorgan”, “West Glamorgan”] unless const_defined?(“WELSH_STATES”)

    ENGLISH_STATES=[“Avon”, “Bedfordshire”, “Berkshire”, “Berwickshire”, “Buckinghamshire”, “Cambridgeshire”, “Cheshire”, “Cleveland”, “Cornwall”, “County Durham”, “Cumbria”, “Derbyshire”, “Devon”, “Dorset”, “East Sussex”, “Essex”, “Gloucestershire”, “Hampshire”, “Herefordshire”, “Hertfordshire”, “Kent”, “Lancashire”, “Leicestershire”, “Lincolnshire”, “London”, “Merseyside”, “Middlesex”, “Norfolk”, “North Humberside”, “North Yorkshire”, “Northamptonshire”, “Northumberland”, “Nottinghamshire”, “Oxfordshire”, “Salop”, “Shropshire”, “Somerset”, “South Humberside”, “South Yorkshire”, “Staffordshire”, “Suffolk”, “Surrey”, “Tyne And Wear”, “Warwickshire”, “West Midlands”, “West Sussex”, “West Yorkshire”, “Wiltshire”, “Worcestershire”] unless const_defined?(“ENGLISH_STATES”)

    NORTHERN_IRISH_STATES=[“County Antrim”, “County Armagh”, “County Down”, “County Fermanagh”, “County Londonderry”, “County Tyrone”] unless const_defined?(“NORTHERN_IRISH_STATES”)

    UK_ISLAND_STATES=[“Channel Isles”, “Isle Of Arran”, “Isle Of Barra”, “Isle Of Benbecula”, “Isle Of Bute”, “Isle Of Canna”, “Isle Of Coll”, “Isle Of Colonsay”, “Isle Of Cumbrae”, “Isle Of Eigg”, “Isle Of Gigha”, “Isle Of Harris”, “Isle Of Iona”, “Isle Of Islay”, “Isle Of Jura”, “Isle Of Lewis”, “Isle Of Man”, “Isle Of Mull”, “Isle Of North Uist”, “Isle Of Rum”, “Isle Of Scalpay”, “Isle Of Skye”, “Isle Of South Uist”, “Isle Of Tiree”, “Isle Of Wight”, “Isles Of Scilly”, “Orkney”, “Shetland”, “Shetland Islands”, “Western Isles”] unless const_defined?(“UK_ISLAND_STATES”)

  • 8. anusha  |  May 23, 2007 at 11:13 am

    can u jst tell me how to retrieve values from the database and do this dynamic drop down lists…. like i have a table called states and a table called cities with foreign key as state_id…
    how do i take values from the database and put both of them in two drop down lists???
    if u can respond quickly… i vud be thankful….

  • 9. Darcy  |  January 21, 2008 at 9:59 pm

    Quick support for US state abbreviations:

    US_ABBREV_STATES=[ “AL”, “AK”, “AZ”, “AR”, “CA”, “CO”, “CT”, “DE”, “DC”, “FL”, “GA”, “HI”, “ID”, “IL”, “IN”, “IA”, “KS”, “KY”, “LA”, “ME”, “MD”, “MA”, “MI”, “MN”, “MS”, “MO”, “MT”, “NE”, “NV”, “NH”, “NJ”, “NM”, “NY”, “NC”, “ND”, “OH”, “OK”, “OR”, “PA”, “RI”, “SC”, “SD”, “TN”, “TX”, “UT”, “VT”, “VA”, “WA”, “WV”, “WI”, “WY” ] unless const_defined?(“US_ABBREV_STATES”)

  • 10. Sudrien  |  April 9, 2008 at 7:04 pm

    Note to novices like myself:

    Make sure to restart Mongrel after installing new plugins, even when running in development mode (as of 2.0.2)

  • 11. yamy  |  August 8, 2008 at 1:20 pm

    JAPAN_STATES=[“Hokkaido”, “Aomori”, “Iwate”, “Miyagi”, “Akita”, “Yamagata”, “Fukushima”, “Ibaraki”, “Tochigi”, “Gunma”, “Saitama”, “Chiba”, “Tokyo”, “Kanagawa”, “Niigata”, “Toyama”, “Ishikawa”, “Fukui”, “Yamanashi”, “Nagano”, “Gifu”, “Shizuoka”, “Aichi”, “Mie”, “Shiga”, “Kyoto”, “Osaka”, “Hyogo”, “Nara”, “Wakayama”, “Tottori”, “Shimane”, “Okayama”, “Hiroshima”, “Yamaguchi”, “Tokushima”, “Kagawa”, “Ehime”, “Kochi”, “Fukuoka”, “Saga”, “Nagasaki”, “Kumamoto”, “Oita”, “Miyazaki”, “Kagoshima”, “Okinawa”] unless const_defined?(“JAPAN_STATES”)

  • 12. Matogen Corporate Web Development  |  August 18, 2008 at 11:59 pm

    SOUTH_AFRICA_STATES=[“Western Cape”,”Northern Cape”,”Free State”,”Gauteng”,”Eastern Cape”,”KwaZulu-Natal”,”Limpopo”,”Mpumalanga”,”North West”] unless const_defined?(”SOUTH_AFRICA_STATES”)

  • 13. KlumsyBird  |  March 12, 2010 at 6:12 am

    I got this working in under 10 min due to some version issues. So the next step is to create city, zipcodes, and not to forget country select. Where’s the big picture on this project? Do we need to have a different field for everything or can we make this into one field that contains all the information. I’m suggesting something like an autosearch where people input zipcode city sate or country and then can chose form there instead of having to do these drop down associations. Yes/no, stupid idea. any comments?

  • 14.  |  April 13, 2013 at 9:31 am

    Hello, I want to subscribe for this web site to take most up-to-date updates, thus where
    can i do it please help.

  • 15. Usefull Thing  |  June 13, 2013 at 12:53 pm

    When you are looking at clearing up acne, you’ll want a “don’t quit attitude”. Acne is amongst the skin conditions that affect both teenagers and adults. Come across out seeing that which you could be applying that’s negative on your own skin.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

Important Notice…

I have moved this blog to . I will continue posting at, not on

Kindly update your book marks to

and .

I am really very sorry for inconvenience.

Comming Soon….

Domain for Sale

(Contact me)

Recent Posts


September 2006
« Aug   Oct »


  • Add to Google
  • Subscribe in NewsGator Online
  • Subscribe with Bloglines
  • WebOnRails on feedburner
  • technorati


Flickr Photos


Red Fox Kit

Fence in Snow 2.1

More Photos

Visitor’s Location

Blog Stats

  • 17,999 hits

%d bloggers like this: