2014-09-23 40 views
1

所以我有2個不同的環節:如何使像團結,各國城市和國家friendly_id網址/得克薩斯州/奧斯汀

united-states/texas/austin 

和:

united-states/colorado/austin-14be76ea-77e2-4f0e-8540-0103ad72cd7a 

我想第二個到是簡單的:

united-states/colorado/austin 

那麼,如何讓friendly_id停止創造獨特的子彈,而是確保城市蛞蝓是獨一無二的時計作用域ry和狀態?

此外,當在我的控制,我該如何找到正確的城市,按國家和國家範圍的?

@city = City.friendly.find(params[:id]) 

這只是看着slu,,並不關心城市是一個嵌套的資源。

這裏是我的設置:

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 

更新:在這裏,你去米哈爾

enter image description here

更新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) %> 
+0

您正確使用哪個版本的FriendlyId?哪個Rails版本? – 2014-09-27 09:01:26

+0

friendly_id(5.0.4),rails(4.1.4) – Abram 2014-09-27 17:05:39

回答

0

有在FriendlyId一個bug導致這一點。請參閱https://github.com/norman/friendly_id/issues/536

如果你踢我**一點也許我會讓我自己最終拉請求一個補丁。

+2

查看我的編輯Michal – Abram 2014-09-25 15:34:14

+1

我正要恢復編輯,直到我看到這個答案。打的好。 – pjmorse 2014-09-25 15:40:56