Forked from https://github.com/purcell/country-select.
Provides a simple helper to get an HTML select list of countries. The list of countries comes from the ISO 3166 standard. While it is a relatively neutral source of country names, it will still offend some users.
Users are strongly advised to evaluate the suitability of this list given their user base.
Install as a gem using
gem install country-select-iso
Or put the following in your Gemfile
gem 'country-select-iso'
CountrySelectIso::countries
will return array of hashes with the following keys:
Key symbol | Description | Example |
---|---|---|
:iso2 | ISO Alpha 2 country code | US |
:iso3 | ISO Alpha 3 country code | USA |
:name | Country name | United States |
:capital | Capital city name | Washington |
:top_level_domain | Top-level internet domain | .us |
:currency_code | Currency code | USD |
:currency_name | Currency name | Dollar |
:phone_code | Phone number country code | 1 |
:postal_code_format | Postal code format | #####-#### |
:postal_code_expr | Postal coe Regexp | ^\d{5}(-\d{4})?$ |
:neighbours_iso2 | Array of neighboring countries ISO2 codes | ['CA','MX','CU'] |
CountrySelectIso::states
will return array of hashes with the following keys: US.TX Texas Texas 4736286
Key symbol | Description | Example |
---|---|---|
:country_iso2 | 2-letter ISO country code | US |
:state_code | State code | TX |
:full_code | [country code 2-letter code].[state code] | US.TX |
:localized_name | Localized state name (not sure about this) | Texas |
:name | ASCII country name | Texas |
country_select("user", "country_name")
<select id="user_country" name="user[country]">
<option value="Canada">Canada</option>
<option value="United Kingdom">United Kingdom</option>
<option value="United States">United States</option>
<option value="" disabled="disabled">-------------</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Aland Islands">Aland Islands</option>
<option value="Albania">Albania</option>
...
</select>
You can set the value of <option> tag to one of the following, by specifying a :value option:
Symbol | Value to bo used | Example |
---|---|---|
:iso2 | ISO Alpha 2 | US |
:iso3 | ISO Alpha 3 | USA |
:name | Country name | United States |
You can also include an array of priority countries that will be shown first. Countries are matched by either iso3, iso2 or country name
country_select("user", "country", value: :iso2, priority_countries: ["RU", "USA", "Albania"])
<select id="user_country" name="user[country]">
<option value="AL">Albania</option>
<option value="RU">Russia</option>
<option value="US">United States</option>
<option value="" disabled="disabled">-------------</option>
<option value="AF">Afghanistan</option>
<option value="AX">Aland Islands</option>
<option value="AL">Albania</option>
...
</select>
state_select("user", "state", value: :full_code, country: "RU", name: :localized_name)
<select id="user_state" name="user[state]">
<option value="RU.01">Adygeya</option>
<option value="RU.03">Altay</option>
<option value="RU.04">Altayskiy</option>
<option value="RU.05">Amur</option>
<option value="RU.06">Arkhangelskaya</option>
<option value="RU.07">Astrakhan</option>
<option value="RU.08">Bashkortostan</option>
<option value="RU.09">Belgorod</option>
...
</select>
Countries list source: http://download.geonames.org/export/dump/countryInfo.txt
States list source: http://download.geonames.org/export/dump/admin1CodesASCII.txt
Copyright (c) 2008 Michael Koziarski, 2012 Igor Shapiro released under the MIT license