ruby-on-rails - 嵌套资源的问题

时间:2010-12-14 16:07:38

标签: ruby-on-rails nested-routes

我遇到嵌套资源问题。

2型号

User => has_many :stuffs

Stuff => belongs_to :user

的routes.rb

map.resources :stuffs

map.resources :users, :has_many => [:stuffs]

当我调用/ users / 1 / stuffs时,它会向我显示相应用户的Stuff。但是当我打电话给/ users / 2 / stuffs时我也得到了这个。它应该返回0“Stuffs”但它不起作用。

MySQL Query from Server
SELECT * FROM `stuffs`

rake routes

stuffs GET    /stuffs(.:format)                         {:action=>"index", :controller=>"stuffs"}
                POST   /stuffs(.:format)                         {:action=>"create", :controller=>"stuffs"}
      new_stuff GET    /stuffs/new(.:format)                     {:action=>"new", :controller=>"stuffs"}
     edit_stuff GET    /stuffs/:id/edit(.:format)                {:action=>"edit", :controller=>"stuffs"}
          stuff GET    /stuffs/:id(.:format)                     {:action=>"show", :controller=>"stuffs"}
                PUT    /stuffs/:id(.:format)                     {:action=>"update", :controller=>"stuffs"}
                DELETE /stuffs/:id(.:format)                     {:action=>"destroy", :controller=>"stuffs"}
    user_stuffs GET    /users/:user_id/stuffs(.:format)          {:action=>"index", :controller=>"stuffs"}
                POST   /users/:user_id/stuffs(.:format)          {:action=>"create", :controller=>"stuffs"}
 new_user_stuff GET    /users/:user_id/stuffs/new(.:format)      {:action=>"new", :controller=>"stuffs"}
edit_user_stuff GET    /users/:user_id/stuffs/:id/edit(.:format) {:action=>"edit", :controller=>"stuffs"}
     user_stuff GET    /users/:user_id/stuffs/:id(.:format)      {:action=>"show", :controller=>"stuffs"}
                PUT    /users/:user_id/stuffs/:id(.:format)      {:action=>"update", :controller=>"stuffs"}
                DELETE /users/:user_id/stuffs/:id(.:format)      {:action=>"destroy", :controller=>"stuffs"}
          users GET    /users(.:format)                          {:action=>"index", :controller=>"users"}
                POST   /users(.:format)                          {:action=>"create", :controller=>"users"}
       new_user GET    /users/new(.:format)                      {:action=>"new", :controller=>"users"}
      edit_user GET    /users/:id/edit(.:format)                 {:action=>"edit", :controller=>"users"}
           user GET    /users/:id(.:format)                      {:action=>"show", :controller=>"users"}
                PUT    /users/:id(.:format)                      {:action=>"update", :controller=>"users"}
                DELETE /users/:id(.:format)                      {:action=>"destroy", :controller=>"users"}
           root        /                                         {:action=>"index", :controller=>"users"}

宝石清单

actionmailer (2.3.8)
actionpack (2.3.8)
activerecord (2.3.8)
activeresource (2.3.8)
activesupport (2.3.8)
arel (2.0.6)
authlogic (2.1.6)
builder (2.1.2)
cgi_multipart_eof_fix (2.5.0)
gem_plugin (0.2.3)
i18n (0.5.0)
mongrel (1.1.5 x86-mingw32)
mysql (2.8.1 x86-mingw32)
paperclip (2.3.7)
rack (1.1.0)
rails (2.3.8)
rake (0.8.7)
tzinfo (0.3.23)

对应的user_id没有where子句。但是如何解决呢?

Rails版本2.3.8

应该像这样工作=> http://guides.rubyonrails.org/routing.html 2.7嵌套资源

希望有人可以提供帮助

4 个答案:

答案 0 :(得分:5)

在你的Stuffs控制器的索引方法中,你是如何收集你的东西的?如果您使用脚手架来创建控制器,它将默认为

@stuffs = Stuff.all

但应该是

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

或其他相似的东西 - 基本上,你收集用户的东西,而不是所有的东西。

答案 1 :(得分:3)

默认情况下,Rails控制器不会过滤您的结果。如上所述,由于您可能在StuffsController中调用Stuff.all,因此它将始终返回所有Stuff对象。

我使用inherited_resources作为我网站上的默认行为。它会自动处理这些关系,并允许您在需要不同的行为时覆盖它们:

https://github.com/josevalim/inherited_resources

答案 2 :(得分:0)

单独的路线不会影响模型。但是,由于传入了用户ID,您可以执行以下操作:

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

答案 3 :(得分:0)

试试这个

map.resources :users do |users|
  users.resources :stuffs
end