2016-01-29 73 views
2

我有以下途徑:如何創建此路線?

resources :articles, except: [:index] do 
    collection do 
     get 'author/:id/articles' => 'articles#index', as: 'index_articles' 
    end 
end 

這導致:

GET /api/v1/articles/:id/author/:id/articles(.:format) 

我怎樣才能把它變成下面?:

GET /api/v1/author/:id/articles(.:format) 
+2

如果你將這個'get'作者/:id/articles'=>'articles#index',作爲''index_articles''在'resources:articles'之外,你將獲得期望的路線。 –

+0

是的,非常感謝你,你會添加這個答案嗎? – Nick

回答

1
# config/routes.rb 
resources :articles, except: [:index] 
resources :authors, path: "author", only: [] do 
    resources :articles, only: :index, on: :member #-> url.com/author/:id/articles 
end 
+0

謝謝,使用'member'而不是其他解決方案是有優勢的。或者這只是公約? – Nick

+0

只是約定,它也是[可擴展](https://en.wikipedia.org/wiki/Extensibility)。如果您希望在之後添加額外的路線,最好將資源範圍擴大到使用單獨路線 –

0

寫這篇

get 'author/:id/articles' => 'articles#index', as: 'index_articles' 

以外resources :articles塊。