- Rails >= 7.0 is now required (Propshaft/importmap-rails, the default modern asset pipeline this release targets, itself requires Rails 7+). Rails 3–6 support (
engine3.rb,railtie.rb) has been removed. If you're on an older Rails, stay on thecountry_state_select3.x series. - Ruby >= 3.1 is now required.
The Ruby API (countries_collection, collect_states, state_options, city_options, etc.) and the legacy jQuery/Chosen JavaScript (vendor/assets/javascript/country_state_select.js.erb) are unchanged and still work exactly as in 3.x, aside from the fixes below. You can upgrade the gem, bump your Rails version if needed, and keep everything else as-is.
cities_collection(the simple_form helper) now actually filters by country. In 3.x it silently ignored the:countrykey infield_namesand only ever looked up cities by state, which meant it never respected the country half of a state/country pair. Duplicate state codes across countries would have picked up the wrong country's cities. If you were relying on that behavior, it's now consistent withcollect_cities(state, country), i.e. it needsfield_namesto include both:stateand:country./find_statesand/find_citiesare HTTP-cached by default (Cache-Control+ETag, 1 day). This is new — 3.x sent no caching headers at all. SetCountryStateSelect.configure { |c| c.cache_duration = nil }to opt out./find_statesand/find_citiesnow go through your configured data source andonly/except/priorityfilters, not thecity-stategem directly. This only changes behavior if you set those new options.
Everything in the README's Configuration section — priority_countries, only/except filtering, flags, dial_codes, localize_names, a pluggable data_source, draw_routes — is new in 4.0 and defaults to off/unchanged, so adopting the gem doesn't require adopting any of it.
You don't have to — the legacy jQuery/Chosen build keeps working — but if you want to drop the jQuery dependency:
- Remove
//= require country_state_selectand//= require chosen-jquery(and the corresponding CSSrequires) from your asset manifest. - Follow the README's JavaScript setup for whichever pipeline you use (importmap+Stimulus is the default for Rails 7/8 apps).
- If you were using
chosen_ui: true, the modern equivalent is Tom Select — Chosen is unmaintained. The Stimulus controller has atom-selectvalue for this; the vanilla/npm build takes anenhancecallback for the same purpose.
Also optional. f.country_select, f.state_select, f.city_select are new form_with/form_for builder methods that don't require simple_form — see the README's Using it in a form section. Your existing simple_form-based views keep working unchanged.