2014-09-23 54 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