그래서 나는 2 개 개의 다른 링크가 있습니다방법/나는 /이 텍사스 주 - 주 같은 도시와 국가에 대한 friendly_id URL을 만들 오스틴을
united-states/texas/austin
과 :
united-states/colorado/austin-14be76ea-77e2-4f0e-8540-0103ad72cd7a
내가 두 번째로 원하는을 단순히 수 : 나는 독특한 슬러그을 만드는 중지하고 대신 그 도시 슬러그가 고유 보장하기 위해 friendly_id 얻는 방법 그래서
united-states/colorado/austin
계산에 의해 범위 때 라이와 주?
또한 내 컨트롤러에서 올바른 도시를 국가 및 주별로 어떻게 찾을 수 있습니까?
@city = City.friendly.find(params[:id])
이것은 단지 슬러그를보고 중첩 된 리소스가되는 도시는 신경 쓰지 않습니다.
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
업데이트 : 여기
내 설정이다 여기마이클 업데이트 2를 이동 : 내 수정을위한 지금
제거 :
resources :homelands, :path => '' do
resources :regions, :path => '' do
resources :cities, :path => ''
end
end
추가 : 링크에 대한
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'
:
<%= link_to region.name, region_path(region.slug, homeland_id: @homeland.slug) %>
어떤 FriendlyId 버전을 사용하고 있습니까? 그리고 어떤 레일 버전? –
friendly_id (5.0.4), 레일 (4.1.4) – Abram