1
我有2種型號:嵌套資源和漂亮的URL
- 體育
- 競爭
在我的routes.rb我通常會做
resources :sports, :only => [:index, :show] do
resources :competitions, , :only => [:index, :show]
end
這給我的網址像/sports/1/competitions/4
但我要的是不:controller/:id
,而是直接將:name
的路徑,像這樣/soccer/euro2012
我可以這樣做,得到的第一部分:
match "/:sports_name" => "sports#show", :as => :sport
而且在我的控制器:
def show
@sport = Sport.find_by_name(params[:sports_name])
end
但就是這樣。我不知道如何獲得嵌套的資源,或者如果我正在做一些根本性的錯誤。