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 @@
<% end %> + + <% if @current_country %> + + <% end %> +