Sonny Black
Sonny Black

Reputation: 1617

How to display states, and cities together in a drop down menu

So I'd like to do something similar to https://www.weddingwire.ca/ specifically the "Where" dropdown.

They have the states, and then the Cities under the states. How might I do something like this in Rails?

At the moment, I have only States/Provinces but would like to include the cities under the provinces. E.g. Ontario, Toronto, Mississauga, Hamilton. etc...

How might I accomplish something similar?

Thanks!

Upvotes: 0

Views: 55

Answers (1)

nickcen
nickcen

Reputation: 1692

Take a look at the simple_form gem https://github.com/plataformatec/simple_form, it support the grouped collection.

f.input :country_id, collection: @continents, as: :grouped_select, group_method: :countries

Then take a look at the select2 https://select2.github.io/examples.html, and also there is a gem call rails-select2 which integrate the select2 into rails.

$(document).ready(function() {
  $("#country_id").select2();
});

Upvotes: 1

Related Questions