所以我有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