diff --git a/app/controllers/cities_controller.rb b/app/controllers/cities_controller.rb
index a0883ff..9148109 100644
--- a/app/controllers/cities_controller.rb
+++ b/app/controllers/cities_controller.rb
@@ -21,6 +21,11 @@ class CitiesController < ApplicationController
@cities = @cities.by_country(@current_country.id) if @current_country
end
+ if params[:state]
+ @current_state = State.friendly.find(params[:state])
+ @cities = @cities.by_state(@current_state.id) if @current_state
+ end
+
@cities = @cities.page(params[:page]).per(12)
respond_to do |format|
diff --git a/app/models/city.rb b/app/models/city.rb
index 38944d0..66e6356 100644
--- a/app/models/city.rb
+++ b/app/models/city.rb
@@ -19,6 +19,7 @@ class City < ApplicationRecord
scope :by_region, ->(region_id) { joins(:country).where(countries: { region_id: region_id }) }
scope :by_country, ->(country_id) { where(country_id: country_id) }
+ scope :by_state, ->(state_id) { where(state_id: state_id) }
scope :active, -> { where(active: true) }
scope :inactive, -> { where(active: false) }
diff --git a/app/models/state.rb b/app/models/state.rb
index f2f8516..fbb303d 100644
--- a/app/models/state.rb
+++ b/app/models/state.rb
@@ -1,4 +1,7 @@
class State < ApplicationRecord
+ extend FriendlyId
+ friendly_id :name, use: :slugged
+
belongs_to :country
has_many :cities
diff --git a/app/views/cities/index.html.erb b/app/views/cities/index.html.erb
index 6db8f84..84e0205 100644
--- a/app/views/cities/index.html.erb
+++ b/app/views/cities/index.html.erb
@@ -47,8 +47,10 @@
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 9346608..5b47f2d 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -67,6 +67,7 @@ en:
search_cities: "Search cities..."
all_regions: "All Regions"
all_countries: "All Countries"
+ all_states: "All States"
all_in: "All in"
showing: "Showing"
weather_arts: "Weather Arts"
diff --git a/db/migrate/20250226011649_add_slug_to_states.rb b/db/migrate/20250226011649_add_slug_to_states.rb
new file mode 100644
index 0000000..dfc93d2
--- /dev/null
+++ b/db/migrate/20250226011649_add_slug_to_states.rb
@@ -0,0 +1,6 @@
+class AddSlugToStates < ActiveRecord::Migration[8.0]
+ def change
+ add_column :states, :slug, :string
+ add_index :states, :slug, unique: true
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 285db5a..741dab5 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[8.0].define(version: 2025_02_11_035423) do
+ActiveRecord::Schema[8.0].define(version: 2025_02_26_011649) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
@@ -205,7 +205,9 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_11_035423) do
t.string "wiki_data_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.string "slug"
t.index ["country_id"], name: "index_states_on_country_id"
+ t.index ["slug"], name: "index_states_on_slug", unique: true
end
create_table "subregions", force: :cascade do |t|