So I have 2 different links:
united-states/texas/austin
and:
united-states/colorado/austin-14be76ea-77e2-4f0e-8540-0103ad72cd7a
I want the second one to be simply:
united-states/colorado/austin
So how do I get friendly_id to stop creating unique slugs, and instead ensure that city slug is unique when scoped by country and state?
Also when in my controller, how do I locate the correct city, scoped by country and state?
@city = City.friendly.find(params[:id])
This just looks at the slug, and doesn't care about city being a nested resource.
Here is my setup:
class City < ActiveRecord::Base
extend FriendlyId
friendly_id :name, :use => :scoped, :scope => [:homeland, :region]
belongs_to :region
belongs_to :homeland
end
class Region < ActiveRecord::Base
extend FriendlyId
friendly_id :name, :use => :scoped, :scope => :homeland
belongs_to :homeland
has_many :cities
end
#Had to use Homeland as Country was in use
class Homeland < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
has_many :regions
has_many :cities, through: :regions
end
Routes.rb
resources :homelands, :path => '' do
resources :regions, :path => '' do
resources :cities, :path => ''
end
end
Update: Here you go Michal
Update 2: My fix for now
Remove:
resources :homelands, :path => '' do
resources :regions, :path => '' do
resources :cities, :path => ''
end
end
Add:
get "local/:id", to: "homelands#show", as: 'homeland'
get "local/:homeland_id/:id", to: "regions#show", as: 'region'
get "local/:homeland_id/:region_id/:id", to: "cities#show", as: 'city'
For Links:
<%= link_to region.name, region_path(region.slug, homeland_id: @homeland.slug) %>