2011-01-09 43 views
2

我試圖創建一個具有許多獎項扶手:的has_many和路由

所以我在routes.rb中這樣的學生:

資源:學生的has_many => [:大獎]

而且我認爲這應該作出這樣我的鏈接,如果我想看到一個學生的獎項:

本地主機:3000 /學生/ 1 /獎項

但我GETT未找到路由錯誤。

我錯過了什麼?

回答

8

你不要在你的路由文件,定義的has_many其在模型中定義:

#routes.rb 
resources :students do 
    resources :awards 
end 

 

#student.rb 
has_many :awards 

當進行嵌套路由,形成塊和巢資源裏面如上所述。您還可以定義額外的路線:

#routes.rb 
resources :students do 
    resources :awards 
    get 'foo' => 'controller#index' # maps to /students/foo 
end 
1
resources :students, :has_many => :awards 

甚至更​​好

resources :students do 
    resources :awards 
end 

也從運行終端「耙路線」會提醒你你破碎的路線:)