2016-11-05 49 views
0

需要生成以下途徑爲Rails的路線格式

/:owner_name/articles 
/:articles/:id 
/owners 
/:owner_name 

我曾嘗試爲,

resources :owners, param: :owner_name, path: '', shallow_path: "", only: [:index, :show] do 
resources :articles, only: [:index,:show] 
end 

但我無法重現相同的

回答

0

也許這是不最好的解決方案,但你可以嘗試以下內容

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