也許這是不最好的解決方案,但你可以嘗試以下內容
resources :articles, only: :show
resources :owners, only: :index
resources :owners, param: :name, path: '', only: :show do
resources :articles, only: :index
end
它產生
Prefix Verb URI Pattern Controller#Action
article GET /articles/:id(.:format) articles#show
owners GET /owners(.:format) owners#index
owner_articles GET /:owner_name/articles(.:format) articles#index
owner GET /:name(.:format) owners#show
UPDATE
上面的代碼有:name
參數,而不是爲:owner_name
路線owners#show
(如果它不管你)。
您可以嘗試手動指定像
resources :articles, only: :show
resources :owners, only: :index
get '/:owner_name', to: 'owners#show'
get '/:owner_name/articles', to: 'articles#index'
參數它會給你
Prefix Verb URI Pattern Controller#Action
article GET /articles/:id(.:format) articles#show
owners GET /owners(.:format) owners#index
GET /:owner_name(.:format) owners#show
GET /:owner_name/articles(.:format) articles#index