States and provinces for Rails' grouped_options_for_select

webdev rails

Wed Aug 18 15:01:35 +0000 2010

My company was needing a state drop down in a Rails app to record license plate states. As a higher-education institution, there is the possibility of Canadian and Mexican vehicles as well. Not finding a Rails-ready hash for the grouped_options_for_select method, I converted an HTML version to a Rails-ready version for my reference and perhaps the benefit of others.

Note: if you use the grouped_options_for_select method, realize that the value isn’t automatically filled in by the ActionView helper for existing method values. You must include the selected_key = @model_object.method parameter

{"United States" =>  
  [["AL", "Alabama (AL)"],  
  ["AK", "Alaska (AK)"],  
  ["AZ", "Arizona (AZ)"],
  ["AR", "Arkansas (AR)"],             
  ["CA", "California (CA)"],
  ["CO", "Colorado (CO)"],
  ["CT", "Connecticut (CT)"],
  ["DE", "Delaware (DE)"],
  ["DC", "District of Columbia (DC)"],    
  ["FL", "Florida (FL)"],
  ["GA", "Georgia (GA)"],
  ["GU", "Guam (GU)"],
  ["HI", "Hawaii (HI)"],
  ["ID", "Idaho (ID)"],
  ["IL", "Illinois (IL)"],
  ["IN", "Indiana (IN)"],
  ["IA", "Iowa (IA)"],
  ["KS", "Kansas (KS)"],
  ["KY", "Kentucky (KY)"],
  ["LA", "Louisiana (LA)"],
  ["ME", "Maine (ME)"],
  ["MD", "Maryland (MD)"],
  ["MA", "Massachusetts (MA)"],
  ["MI", "Michigan (MI)"],
  ["MN", "Minnesota (MN)"],
  ["MS", "Mississippi (MS)"],
  ["MO", "Missouri (MO)"],
  ["MT", "Montana (MT)"],
  ["NE", "Nebraska (NE)"],
  ["NV", "Nevada (NV)"],
  ["NH", "New Hampshire (NH)"],
  ["NJ", "New Jersey (NJ)"],
  ["NM", "New Mexico (NM)"],
  ["NY", "New York (NY)"],
  ["NC", "North Carolina (NC)"],
  ["ND", "North Dakota (ND)"],
  ["OH", "Ohio (OH)"],
  ["OK", "Oklahoma (OK)"],
  ["OR", "Oregon (OR)"],
  ["PA", "Pennyslvania (PA)"],
  ["PR", "Puerto Rico (PR)"],
  ["RI", "Rhode Island (RI)"],
  ["SC", "South Carolina (SC)"],
  ["SD", "South Dakota (SD)"],
  ["TN", "Tennessee (TN)"],
  ["TX", "Texas (TX)"],
  ["UT", "Utah (UT)"],
  ["VT", "Vermont (VT)"],
  ["VA", "Virginia (VA)"],
  ["VI", "Virgin Islands (VI)"],
  ["WA", "Washington (WA)"],
  ["WV", "West Virginia (WV)"],
  ["WI", "Wisconsin (WI)"],
  ["WY", "Wyoming (WY)"]],
"Canada" =>
  [["AB", "Alberta (AB)"],
  ["BC", "British Columbia (BC)"],
  ["MB", "Manitoba (MB)"],
  ["NB", "New Brunswick (NB)"],
  ["NL", "Newfoundland and Labrador (NL)"],
  ["NT", "Northwest Territories (NT)"],
  ["NS", "Nova Scotia (NS)"],
  ["NU", "Nunavut (NU)"],
  ["PE", "Prince Edward Island (PE)"],
  ["SK", "Saskatchewan (SK)"],
  ["ON", "Ontario (ON)"],
  ["QC", "Quebec (QC)"],
  ["YT", "Yukon (YT)"]],
"Mexico" =>
  [["AGS", "Aguascalientes (AGS)"],
  ["BCN", "Baja California Norte (BCN)"],
  ["BCS", "Baja California Sur (BCS)"],
  ["CAM", "Campeche (CAM)"],
  ["CHIS", "Chiapas (CHIS)"],
  ["CHIH", "Chihuahua (CHIH)"],
  ["COAH", "Coahuila (COAH)"],
  ["COL", "Colima (COL)"],
  ["DF", "Distrito Federal (DF)"],
  ["DGO", "Durango (DGO)"],
  ["GTO", "Guanajuato (GTO)"],
  ["GRO", "Guerrero (GRO)"],
  ["HGO", "Hidalgo (HGO)"],
  ["JAL", "Jalisco (JAL)"],
  ["EDM", "México - Estado de (EDM)"],
  ["MICH", "Michoacán (MICH)"],
  ["MOR", "Morelos (MOR)"],
  ["NAY", "Nayarit (NAY)"],
  ["NL", "Nuevo León (NL)"],
  ["OAX", "Oaxaca (OAX)"],
  ["PUE", "Puebla (PUE)"],
  ["QRO", "Querétaro (QRO)"],
  ["QROO", "Quintana Roo (QROO)"],
  ["SLP", "San Luis Potosí (SLP)"],
  ["SIN", "Sinaloa (SIN)"],
  ["SON", "Sonora (SON)"],
  ["TAB", "Tabasco (TAB)"],
  ["TAMPS", "Tamaulipas (TAMPS)"],
  ["TLAX", "Tlaxcala (TLAX)"],
  ["VER", "Veracruz (VER)"],
  ["YUC", "Yucatán (YUC)"],
  ["ZAC", "Zacatecas (ZAC)"]]
}
blog comments powered by Disqus