2011-05-13 183 views
3

我有一個community_users模型,我以下列方式路線:的Rails 3路由

resources :communities do 
    resources :users 
end 

這將創建路線/communities/:id/users/

我想配置此路由,以便只顯示具有相應:id的社區名稱。

換句話說,如果一個社區有 '1' 的ID和名稱爲 '軌道愛好者' - 的路線將是:

/rails-lovers 

,而不是:

/communities/1/users/ 

回答

3

我不十分肯定,如果這是你在找什麼,但:

一種選擇是創建路線

match ':community_name' => 'users#show_users_for_community' 

然後在UsersController有

def show_users_for_community 
    @community = Community.find_by_name(params[:community_name]) 
    <do what you need to do here> 
end 

我不知道如果路由將匹配了過多的網址或不 - 這是一個非常普遍的路線。所以如果你這樣做,也許把它放在你的路線文件中。