Plugin: state_select, generate drop down selection box for states


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…

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

Usage:

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

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 http://opensvn.csie.org/state_select/trunk/

Install plugin by

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts
Lib file state_select.rb
12 Balls Problem

Reader Comments

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”)
end

class ActionView::Helpers::InstanceTag
Index: README
===================================================================
— README (revision 1)
+++ README (working copy)
@@ -6,7 +6,7 @@
Usage:
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.

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

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”)

Thanks Vicent,
I have added this to plugin itself.

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.

Thanks,
Ryan

UGANDA_STATES=[”Abim”, “Adjumani”, “Amolatar”, “Amuria”, “Apac”, “Arua”, “Budaka”, “Bugiri”, “Bukwa”, “Bulisa”, “Bundibugyo”, “Bushenyi”, “Busia”, “Busiki”, “Butaleja”, “Dokolo”, “Gulu”, “Hoima”, “Ibanda”, “Iganga”, “Jinja”, “Kaabong”, “Kabale”, “Kabarole”, “Kaberamaido”, “Kabingo”, “Kalangala”, “Kaliro”, “Kampala”, “Kamuli”, “Kamwenge”, “Kanungu”, “Kapchorwa”, “Kasese”, “Katakwi”, “Kayunga”, “Kibale”, “Kiboga”, “Kilak”, “Kiruhura”, “Kisoro”, “Kitgum”, “Koboko”, “Kotido”, “Kumi”, “Kyenjojo”, “Lira”, “Luwero”, “Manafwa”, “Maracha”, “Masaka”, “Masindi”, “Mayuge”, “Mbale”, “Mbarara”, “Mityana”, “Moroto”, “Moyo”, “Mpigi”, “Mubende”, “Mukono”, “Nakapiripirit”, “Nakaseke”, “Nakasongola”, “Nebbi”, “Ntungamo”, “Oyam”, “Pader”, “Pallisa”, “Rakai”, “Rukungiri”, “Sembabule”, “Sironko”, “Soroti”, “Tororo”, “Wakiso”, “Yumbe”] unless const_defined?(”UGANDA_STATES”)

Thanks Chuck Phillips,
I have added this to the plugin itself.

FRANCE_STATES=[”Alsace”,”Aquitaine”,”Auvergne”,”Bourgogne”,”Bretagne”,”Centre”,”Champagne-Ardenne”,”Corse”,”Franche-Comté”,”Île-de-France”,”Languedoc-Roussillon”,”Limousin”,”Lorraine”,”Midi-Pyrénées”,”Nord-Pas-de-Calais”,”Basse-Normandie”,”Haute-Normandie”,”Pays de la Loire”,”Picardie”,”Poitou-Charentes”,”Provence-Alpes-Côte d’Azur”,”Rhône-Alpes”] unless const_defined?(”FRENCH_STATES”)

Thanks, this works great….

Actually… its not setting the selected value for my object.

Am I missing something?

To make this AJAX like, I had to do a little extra work. For instance, the country Germany requires the form helper to say GERMAN, United States to say US, etc. It would be nice if the constant was a capital version of the country and if there was no country, it would show a text field for entry.

0.5,
:update=>"state_select",
:url=> {:action=>'state_select', :only_path=>false},
:with=>"'country=' + encodeURIComponent(value)" %>

would link to a partial


instead of…

"text" %>

I put the code:
http://ianconnor.blogspot.com/2007/03/ajax-stateselect.html
as the html didn’t get escaped and/or stripped the ruby code.

BRAZIL_STATES=[”Acre”
“Alagoas”
“Amapá”
“Amazonas”
“Bahia”
“Ceará”
“Distrito Federal”
“Goiás”
“Espírito Santo”
“Maranhão”
“Mato Grosso”
“Mato Grosso do Sul”
“Minas Gerais”
“Pará”
“Paraiba”
“Paraná”
“Pernambuco”
“Piauí”
“Rio de Janeiro”
“Rio Grande do Norte”
“Rio Grande do Sul”
“Rondônia”
“Rorâima”
“São Paulo”
“Santa Catarina”
“Sergipe”
“Tocantins”] unless const_defined?(”BRAZIL_STATES”)

sorry, last post wasn’t nice…
i forgot the commas… hehe
here is the good one:

BRAZIL_STATES=[”Acre”, “Alagoas”, “Amapá”, “Amazonas”, “Bahia”,”Ceará”, “Distrito Federal”, “Goiás”, “Espírito Santo”, “Maranhão”, “Mato Grosso”, “Mato Grosso do Sul”, “Minas Gerais”, “Pará”, “Paraiba”, “Paraná”, “Pernambuco”, “Piauí”, “Rio de Janeiro”, “Rio Grande do Norte”, “Rio Grande do Sul”, “Rondônia”, “Rorâima”, “São Paulo”, “Santa Catarina”, “Sergipe”, “Tocantins”] unless const_defined?(”BRAZIL_STATES”)

Thanks Kaith,it works great but I faced
one problem with it ,options={} and html_options ={} is does not work.How can I put class or id for it.

Here is US states with 2 char codes:
US_WITH_CODE_STATES = [[’Alabama’, ‘AL’], [’Alaska’, ‘AK’], [’Arizona’, ‘AZ’], [’Arkansas’, ‘AR’], [’California’, ‘CA’], [’Colorado’, ‘CO’], [’Connecticut’, ‘CT’], [’Delaware’, ‘DE’], [’District Of Columbia’, ‘DC’], [’Florida’, ‘FL’],[’Georgia’, ‘GA’],[’Hawaii’, ‘HI’], [’Idaho’, ‘ID’], [’Illinois’, ‘IL’], [’Indiana’, ‘IN’], [’Iowa’, ‘IA’], [’Kansas’, ‘KS’], [’Kentucky’, ‘KY’], [’Louisiana’, ‘LA’], [’Maine’, ‘ME’], [’Maryland’, ‘MD’], [’Massachusetts’, ‘MA’], [’Michigan’, ‘MI’], [’Minnesota’, ‘MN’], [’Mississippi’, ‘MS’], [’Missouri’, ‘MO’], [’Montana’, ‘MT’], [’Nebraska’, ‘NE’], [’Nevada’, ‘NV’], [’New Hampshire’, ‘NH’], [’New Jersey’, ‘NJ’], [’New Mexico’, ‘NM’], [’New York’, ‘NY’], [’North Carolina’, ‘NC’], [’North Dakota’, ‘ND’], [’Ohio’, ‘OH’], [’Oklahoma’, ‘OK’], [’Oregon’, ‘OR’], [’Pennsylvania’, ‘PA’], [’Rhode Island’, ‘RI’], [’South Carolina’, ‘SC’], [’South Dakota’, ‘SD’], [’Tennessee’, ‘TN’], [’Texas’, ‘TX’], [’Utah’, ‘UT’], [’Vermont’, ‘VT’], [’Virginia’, ‘VA’], [’Washington’, ‘WA’], [’West Virginia’, ‘WV’], [’Wisconsin’, ‘WI’], [’Wyoming’, ‘WY’]] unless const_defined?(”US_WITH_CODE_STATES”)

This is a great timesaver. But is there a way for it to take the value from the object/method passed to it? i.e. if i am updating a record, i would like it to start at the existing value, rather than the first item in the list. (this is how country_select works.)

Hey - just wanted to second what Timur said. Having the state postal codes is a big help. I used that as he posted, except I named it “US_POSTAL_STATES”. That seemed a bit more semantic to me.

Thanks - it’s a great help!

I put together a list of just the postal codes - no state names - for the US. List was compiled from the USPS official postal codes list of the US and major outlying areas.

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

In response to Matt I played with this for about an hour and a half before I gained any functionality. Here’s my code (my object is a TV Station, “station” and my method is “state”)

State:

I inserted a blank option field so if no state is given it doesn’t get set to AK or Alabama by default. The state will get set if it exists (for an edit type function) but if it does not exist it will be blank.

PS I had to restart the web server after modifying the state_select.rb file in the plugin.

Hi, I am getting this error:

ActionView::TemplateError (uninitialized constant ActionView::Base::CompiledTemplates::US) on line

But this works:

POLAND_STATES=[”Dolnośląskie”, “Kujawsko-Pomorskie”, “Lubelskie”, “Lubuskie”, “Łódzkie”, “Małopolskie”, “Mazowieckie”, “Opolskie”, “Podkarpackie”, “Podlaskie”, “Pomorskie”, “Śląskie”, “Świętokrzyskie”, “Warmińsko-Mazurskie”, “Wielkopolskie”, “Zachodniopomorskie”] unless const_defined?(”POLAND_STATES”)

Canada does NOT have “District of Columbia and NEEDS “Yukon”

Counties for Republic of Ireland: IE_STATES=[”Carlow”,”Cavan”,”Cork”,”Clare”,”Donegal”,”Dublin”,”Galway”,”Kerry”,”Kildare”,”Kilkenny”,”Laois”,”Leitrim”,”Limerick”,”Longford”,”Louth”,”Mayo”,”Meath”,”Monaghan”,”Offaly”,”Roscommon”,”Sligo”,”Tipperary”,”Waterford”,”Westmeath”,”Wexford”,”Wicklow”] unless const_defined?(”IE_STATES”)
Counties for Northern Ireland:
NI_STATES=[”Antrim”,”Armagh”,”Derry”,”Down”, “Fermanagh”,”Tyrone”] unless const_defined?(”NI_STATES”)

Might I suggest changing your current US_STATES to US_UNABBREV_STATES and adding the following:

US_STATES=[
[ “Alabama”, “AL” ],
[ “Alaska”, “AK” ],
[ “Arizona”, “AZ” ],
[ “Arkansas”, “AR” ],
[ “California”, “CA” ],
[ “Colorado”, “CO” ],
[ “Connecticut”, “CT” ],
[ “Delaware”, “DE” ],
[ “District Of Columbia”, “DC” ],
[ “Florida”, “FL” ],
[ “Georgia”, “GA” ],
[ “Hawaii”, “HI” ],
[ “Idaho”, “ID” ],
[ “Illinois”, “IL” ],
[ “Indiana”, “IN” ],
[ “Iowa”, “IA” ],
[ “Kansas”, “KS” ],
[ “Kentucky”, “KY” ],
[ “Louisiana”, “LA” ],
[ “Maine”, “ME” ],
[ “Maryland”, “MD” ],
[ “Massachusetts”, “MA” ],
[ “Michigan”, “MI” ],
[ “Minnesota”, “MN” ],
[ “Mississippi”, “MS” ],
[ “Missouri”, “MO” ],
[ “Montana”, “MT” ],
[ “Nebraska”, “NE” ],
[ “Nevada”, “NV” ],
[ “New Hampshire”, “NH” ],
[ “New Jersey”, “NJ” ],
[ “New Mexico”, “NM” ],
[ “New York”, “NY” ],
[ “North Carolina”, “NC” ],
[ “North Dakota”, “ND” ],
[ “Ohio”, “OH” ],
[ “Oklahoma”, “OK” ],
[ “Oregon”, “OR” ],
[ “Pennsylvania”, “PA” ],
[ “Rhode Island”, “RI” ],
[ “South Carolina”, “SC” ],
[ “South Dakota”, “SD” ],
[ “Tennessee”, “TN” ],
[ “Texas”, “TX” ],
[ “Utah”, “UT” ],
[ “Vermont”, “VT” ],
[ “Virginia”, “VA” ],
[ “Washington”, “WA” ],
[ “West Virginia”, “WV” ],
[ “Wisconsin”, “WI” ],
[ “Wyoming”, “WY” ] ] unless const_defined?(”US_STATES”)

Hey guys,
I’m trying to have a selected state and a none option to the state_select. I wasn’t successful getting that to work. I tried everything but no luck. could you give me an example please.

Thanks

Hi Maged Makled,

Try this:
<%= state_select 'user', 'state', 'US', :selected=>‘New York’ %>

thanks

Hi,
Good time saving article.

Thanks,

[…] Web On Rails - » Plugin: state_select, generate drop down selection box for states | Its all about RubyOnRails(ROR) Plugin: state_select, generate drop down selection box for states (tags: rails plugin plugins state_select rubyonrails) […]

MEXICO_STATES = [”Aguascalientes”, “Baja California”, “Baja California Sur”, “Campeche”, “Chiapas”, “Chihuahua”, “Coahuila”, “Colima”, “Distrito Federal”, “Durango”, “Guanajuato”, “Guerrero”, “Hidalgo”, “Jalisco”, “Michoacán”, “Morelos”, “México”, “Nayarit”, “Nuevo León”, “Oaxaca”, “Puebla”, “Querétaro”, “Quintana Roo”, “San Luis Potosí”, “Sinaloa”, “Sonora”, “Tabasco”, “Tamaulipas”, “Tlaxcala”, “Veracruz”, “Yucatán”, “Zacatecas”]

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.