我正在尝试让我的路线像这样工作:
/articles/<category slug>/<article slug>
我正在使用:
ruby '2.1.2'
gem 'rails', '4.1.4'
gem "friendly_id", "~> 5.0.1"
我的类别中有很多文章
网址结构现在是:
/类别/
/文章/
因为我的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
这是我的category.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>
这正是我想要的。它是从mbillard的答案扩展而来的:
get "/articles", to: "articles#index"
resources :categories, path: 'articles' do
resources :articles, path: '', only: [:show]
end
resources :articles, only: [:index, :new, :edit, :create, :update, :destroy]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句