2017-04-24 126 views
0

根據其屬性,如何使資源的路由足夠靈活?Rails路由:取決於屬性的靈活資源路徑

例如,

resources :articles, param: :article_slug do 
    member do 
     resources :comments 
    end 
end 

Article具有title, category, slug, etc

屬性但客戶端想要認爲類別必須存在於URL中的第一,如:/entertainment/articles/:article_slug/comments/:id/sports/articles/:article_slug/comments/:id。我不知道這個方法的正確方法是什麼。

+0

你現在有什麼網址?在運行'耙路線'後 –

+0

我只是'/ articles /:article_slug/comments /:id' –

回答

0

嘗試

scope path: ':category' do 
    resources :articles, param: :article_slug do 
     resources :comments 
     end 
    end 
end 

並以這種方式產生的URL,

article_comment_path(@article, @comment, category: @article.category) 
+0

是的,它可以工作,但類別是'Article'的屬性。我試圖使用一些lambda方法,但它不起作用。 –

+0

@JoseMarieAntonioMiñoza我已經更新了答案,你可以查看它。 –

+0

它的工作原理。謝謝。 :) –