开发者

Rails: has_many and routing

开发者 https://www.devze.com 2023-02-03 02:36 出处:网络
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]

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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号