Rails: has_many and routing
I am trying to create a student that has many awards
So I have something like this in routes.rb:
resources :students, has_many => [:awards]
And I think this should make my link like this if I want to see the awards for a student:
localhost:3000/students/1/awards
B开发者_如何转开发ut I am getting a route not found error.
What am I missing?
You don't define the has_many in your routes file, its defined in your model:
#routes.rb
resources :students do
resources :awards
end
#student.rb
has_many :awards
When making nested routes, you form a block and nest the resources inside as above. You can also define additional routes:
#routes.rb
resources :students do
resources :awards
get 'foo' => 'controller#index' # maps to /students/foo
end
resources :students, :has_many => :awards
or even better
resources :students do
resources :awards
end
also running 'rake routes' from terminal would have alerted you to your broken routes :)
精彩评论