Rails:嵌套的命名空间资源路由

时间:2009-03-26 10:32:38

标签: ruby-on-rails routing

map.resources :users
map.namespace :formulator do |formulator|
    formulator.resources :submissions
end

我希望提交的内容是用户的嵌套资源,但我不知道该怎么做,因为它是命名空间的。

2 个答案:

答案 0 :(得分:1)

你有没有尝试过:

 map.resources :users
 map.namespace :formulator do |formulator|
   formulator.resources :submissions
   map.resources :users, :has_many => :submissions
 end

或者

 map.resources :users
 map.namespace :formulator do |formulator|
   formulator.resources :submissions
 end
 map.resources :users, :has_many => :submissions

如果没有,请尝试之后运行rake routes以查看其生成的路由。

答案 1 :(得分:1)

你应该可以这样做:

map.resources :users, :has_many => :submissions

这会给你这样的路线:

               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"}
    user_submissions GET    /users/:user_id/submissions(.:format)          {:action=>"index", :controller=>"submissions"}
                     POST   /users/:user_id/submissions(.:format)          {:action=>"create", :controller=>"submissions"}
 new_user_submission GET    /users/:user_id/submissions/new(.:format)      {:action=>"new", :controller=>"submissions"}
edit_user_submission GET    /users/:user_id/submissions/:id/edit(.:format) {:action=>"edit", :controller=>"submissions"}
     user_submission GET    /users/:user_id/submissions/:id(.:format)      {:action=>"show", :controller=>"submissions"}
                     PUT    /users/:user_id/submissions/:id(.:format)      {:action=>"update", :controller=>"submissions"}
                     DELETE /users/:user_id/submissions/:id(.:format)      {:action=>"destroy", :controller=>"submissions"}