14

Is there a country and city gem where user will be able to select a country and based on selected country he will select a city?

I get multiple solution but they all support states not cities

gem 'country_select'
gem 'countries'
gem 'carmen-rails'

I want a list of all countries on dropdown and when I select any country then all cities of country will appear in cities dropdown. Is this possible through any gem?

  • The reason someone has downvoted your question is most probably because questions asking for library recommendations are generally considered off-topic for SO as stated by the [What topics can I ask about here? (list item 4) in the help center](http://stackoverflow.com/help/on-topic). – ndnenkov Apr 29 '16 at 05:26

4 Answers4

27

Use the aptly named city-state gem.


As seen by the README, you can do:

CS.cities(state, country)

If you want all cities in a given country, regardless of state, you could do:

CS.states(country).keys.flat_map { |state| CS.cities(state, country) }

To list all countries:

CS.countries

The cool thing is that it uses data from MaxMind so you can be sure the list will be up to date. To refresh your list, you can run:

CS.update

The only drawback is that it requires rails (active support specifically) to run, but this isn't a problem in your case.

ndnenkov
  • 35,425
  • 9
  • 72
  • 104
  • I'm not sure why this answer hasn't been upvoted more. None of the other answers seem to address what the OP asked for -- a list of *cities*, not states or countries... – GoGoCarl Apr 28 '16 at 23:05
  • @GoGoCarl on other hand people downvote my question and I don't know whats wrong in it –  Apr 29 '16 at 04:18
6

You can try jQuery plugin - bootstrap state picker

After including plugin inside your application

You can use it like this(example from http://bootstrapformhelpers.com/state):

<select id="countries_states1" class="form-control bfh-countries" data-country="US"></select>

<select class="form-control bfh-states" data-country="countries_states1"></select>
yozzz
  • 1,267
  • 1
  • 22
  • 30
5

You can use country picker jQuery plugins of Bootstrap as following:

<select class="form-control bfh-countries" data-country="US"></select>
Dharmesh Rupani
  • 1,029
  • 2
  • 12
  • 22
5

My recommendation is that unless you can be 100% certain the list is accurate and complete (and I'm skeptical that that is possible), don't do it.

I've registered on web sites in Singapore and other places where I had to select from a dropdown list of cities that did not include my town (and many other) correct towns (I live in the USA). I was forced to select an incorrect value pretty far away because the list was quite incomplete and it would not permit me to not select anything.

At the very least, if you want to provide choices, permit the user to enter a value not included in those choices.

Keith Bennett
  • 4,722
  • 1
  • 25
  • 35
  • 1
    I can't claim to know whether the sites I mentioned use external services, nor whether or not all external services are thorough and accurate...but it's certainly something to question and verify. – Keith Bennett Apr 28 '16 at 17:42
  • Yes, maybe only use a list for countries, as that doesn't change often, but I intend to use the list of cities and states for auto complete in a free form text box. – Chloe Apr 11 '17 at 19:19