我遇到一些路由问题。
我有这些模型:
class Content < ApplicationRecord
belongs_to :section
end
class Section < ApplicationRecord
has_many :contents
end
我使用的是FriendlyID Gem,所以我在每个模型中都有一个slug参数。
我想要一个像
这样的url内容https://example.com/section_slug/content_slug
我试试
resources :sections, only: [:show] do
resources :contents, only: [:show]
end
但是结果是
/sections/:section_id/contents/:id(.:format)
我试试
match '/:category/:title', via: [:get], to: 'contents#show'
但它不工作
问题在哪里?
你可以像这样在你的路由中使用path
选项:
resources :sections, only: [:show], path: '' do
resources :contents, only: [:show], path: ''
end
这将用空字符串替换/sections/
,/contents/
也是如此,所以你最终会得到一个url,如:/:section_id/:content_id/