Rails 3 nested resources short name?

后端 未结 3 2042
攒了一身酷
攒了一身酷 2021-02-15 23:19

I\'m in the process of upgrading a Rails 2.3 app to Rails 3. In the Rails 2.3 router, it was possible to set a :name_prefix of nil on nested resources to get a sho

3条回答
  •  时光说笑
    2021-02-15 23:52

    There's the :shallow option (see the documentation), but I'm not sure it fits your use case:

    resources :sites, :shallow => true
      resources :groups do
        resources :tests
      end
    end
    

    It has the disadvantage of creating a bunch of additional routes:

       group_tests GET    /groups/:group_id/tests(.:format)        {:action=>"index", :controller=>"tests"}
                   POST   /groups/:group_id/tests(.:format)        {:action=>"create", :controller=>"tests"}
    new_group_test GET    /groups/:group_id/tests/creer(.:format)  {:action=>"new", :controller=>"tests"}
         edit_test GET    /tests/:id/modifier(.:format)            {:action=>"edit", :controller=>"tests"}
              test GET    /tests/:id(.:format)                     {:action=>"show", :controller=>"tests"}
                   PUT    /tests/:id(.:format)                     {:action=>"update", :controller=>"tests"}
                   DELETE /tests/:id(.:format)                     {:action=>"destroy", :controller=>"tests"}
       site_groups GET    /sites/:site_id/groups(.:format)         {:action=>"index", :controller=>"groups"}
                   POST   /sites/:site_id/groups(.:format)         {:action=>"create", :controller=>"groups"}
    new_site_group GET    /sites/:site_id/groups/creer(.:format)   {:action=>"new", :controller=>"groups"}
        edit_group GET    /groups/:id/modifier(.:format)           {:action=>"edit", :controller=>"groups"}
             group GET    /groups/:id(.:format)                    {:action=>"show", :controller=>"groups"}
                   PUT    /groups/:id(.:format)                    {:action=>"update", :controller=>"groups"}
                   DELETE /groups/:id(.:format)                    {:action=>"destroy", :controller=>"groups"}
             sites GET    /sites(.:format)                         {:action=>"index", :controller=>"sites"}
                   POST   /sites(.:format)                         {:action=>"create", :controller=>"sites"}
          new_site GET    /sites/creer(.:format)                   {:action=>"new", :controller=>"sites"}
         edit_site GET    /sites/:id/modifier(.:format)            {:action=>"edit", :controller=>"sites"}
              site GET    /sites/:id(.:format)                     {:action=>"show", :controller=>"sites"}
                   PUT    /sites/:id(.:format)                     {:action=>"update", :controller=>"sites"}
                   DELETE /sites/:id(.:format)                     {:action=>"destroy", :controller=>"sites"}
    

    Besides, the shallow nesting only applies to the following routes: :show, :edit, :update, :destroy.

提交回复
热议问题