所以我有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
更新:在这里,你去米哈尔
更新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?哪个Rails版本? – 2014-09-27 09:01:26
friendly_id(5.0.4),rails(4.1.4) – Abram 2014-09-27 17:05:39