How to get list of all countries and cities in rails?
Asked Answered
S

4

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?

Stele answered 20/4, 2016 at 7:3 Comment(1)
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.Richmal
R
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.

Richmal answered 28/4, 2016 at 14:25 Comment(2)
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...Beginner
@Beginner on other hand people downvote my question and I don't know whats wrong in itStele
I
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>
Immodest answered 20/4, 2016 at 21:42 Comment(0)
E
5

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

<select class="form-control bfh-countries" data-country="US"></select>
Eventempered answered 27/4, 2016 at 1:15 Comment(1)
Any link related to that?Stele
H
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.

Helsinki answered 27/4, 2016 at 9:54 Comment(2)
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.Helsinki
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.Monty

© 2022 - 2024 — McMap. All rights reserved.