我試圖讓我的路線是這樣工作的:Rails的友好ID定義路由
/articles/<category slug>/<article slug>
我使用:
ruby '2.1.2'
gem 'rails', '4.1.4'
gem "friendly_id", "~> 5.0.1"
我有了很多文章
類別現在的url結構是:
/categories/
/用品/
,因爲我的routes.rb文件看起來像這樣:
resources :categories
resources :articles
我article.rb文件:
class Article < ActiveRecord::Base
belongs_to :category
extend FriendlyId
friendly_id :slug_candidates, use: [:slugged, :globalize]
def slug_candidates
[
:name
]
end
end
這裏是我的類別。 rb:
class Category < ActiveRecord::Base
has_many :articles
extend FriendlyId
friendly_id :slug_candidates, use: [:slugged, :globalize]
# Try building a slug based on the following fields in
# increasing order of specificity.
def slug_candidates
[
:name
]
end
end
如果我做這樣一個嵌套的路線:
resources :categories do
resources :articles
end
則結構變得/categories/<category slug>/articles/<article slug>