ruby-on-rails - Problem with Nested Resources

天涯浪子 提交于 2019-12-04 20:33:13

In your Stuffs controller's index method, how are you collecting your stuffs? If you used a scaffold to create the controller it will default to something like

@stuffs = Stuff.all

but should be something along the lines of

@user = User.find(params[:user_id])
@stuffs = @user.stuffs

or something to that effect--basically, you're collecting the user's stuff, not all stuff.

Rails controllers will not filter your results by default. As above, since you're probably calling Stuff.all in your StuffsController, it will always return all Stuff objects.

I use inherited_resources for the default behaviour on my sites. It handles these relationships automatically and lets you override it when you want different behaviour:

https://github.com/josevalim/inherited_resources

Routes alone don't affect the model. Since the user id is passed in, though, you can do:

User.find(params[:user_id]).stuffs

Try this

map.resources :users do |users|
  users.resources :stuffs
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!