2012-07-11 82 views
0

我有兩個資源:commentscommunities,並且每個註釋與一個或多個社區相關聯。的Rails 3:路線設計用於顯示過濾列表

我的計劃是讓網址/comments顯示所有評論,/comments/:community_name僅顯示來自給定社區的評論。 URL應到`評論#指數」動作來取得一套從Comment模型評論路線(使用named scope)。

我怎樣才能產生這些路線? (或者如果有更合適的路線設計,請告訴我。)

如果我嘗試下面的嵌套路線,看來我需要提供註釋ID,即\comment\:comment_id\communities\:community_id

#routes.rb 
resources :comments, only: [:index, :create, :destroy] 
resources :communities, only: [:index, :new, :create, :destroy] 
resources :comments do 
    resources :communities 
end 

注意:社區資源必須是獨立資源,因爲我需要操作來查看,添加和刪除社區。

回答

0

取消註釋的代碼#routes.rb下面一行,檢查:
match ':controller(/:action(/:id))(.:format)'

0
resources :comments, only: [:index, :create, :destroy] do 
    collection do 
    resources :communities, only: [:index, :new, :create, :destroy] 
    end 
end 

此路線聲明會給你喜歡的網址:

comments/communities 
comments/community/1, etc 

如果你想使用的社區名稱社區ID,你會想要做的是爲您的資源,那麼它最好使用一個漂亮的寶石像friendly_id爲你的資源。