Rails 3 nested resources short name?

后端 未结 3 2029
攒了一身酷
攒了一身酷 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-16 00:16

    I wanted something like this too, clearly it feels redundant to type the prefixes in named routes all the time for resources that only exist as nested. I was able to get what I wanted using scope, I think this is what you sought too:

    resources :sites
    
    scope :path => '/sites/:site_id' do
      resources :groups, :controller => :url_groups do
        post :clone, :on => :member
      end
    end
    
    scope :path => '/sites/:site_id/groups/:group_id' do
      resources :tests, :controller => :test_runs do
        get :latest, :on => :collection
      end
    end
    

    The rake routes output:

           sites GET        /sites(.:format)                                             sites#index
                 POST       /sites(.:format)                                             sites#create
        new_site GET        /sites/new(.:format)                                         sites#new
       edit_site GET        /sites/:id/edit(.:format)                                    sites#edit
            site GET        /sites/:id(.:format)                                         sites#show
                 PUT        /sites/:id(.:format)                                         sites#update
                 DELETE     /sites/:id(.:format)                                         sites#destroy
     clone_group POST       /sites/:site_id/groups/:id/clone(.:format)                   url_groups#clone
          groups GET        /sites/:site_id/groups(.:format)                             url_groups#index
                 POST       /sites/:site_id/groups(.:format)                             url_groups#create
       new_group GET        /sites/:site_id/groups/new(.:format)                         url_groups#new
      edit_group GET        /sites/:site_id/groups/:id/edit(.:format)                    url_groups#edit
           group GET        /sites/:site_id/groups/:id(.:format)                         url_groups#show
                 PUT        /sites/:site_id/groups/:id(.:format)                         url_groups#update
                 DELETE     /sites/:site_id/groups/:id(.:format)                         url_groups#destroy
    latest_tests GET        /sites/:site_id/groups/:group_id/tests/latest(.:format)      test_runs#latest
           tests GET        /sites/:site_id/groups/:group_id/tests(.:format)             test_runs#index
                 POST       /sites/:site_id/groups/:group_id/tests(.:format)             test_runs#create
        new_test GET        /sites/:site_id/groups/:group_id/tests/new(.:format)         test_runs#new
       edit_test GET        /sites/:site_id/groups/:group_id/tests/:id/edit(.:format)    test_runs#edit
            test GET        /sites/:site_id/groups/:group_id/tests/:id(.:format)         test_runs#show
                 PUT        /sites/:site_id/groups/:group_id/tests/:id(.:format)         test_runs#update
                 DELETE     /sites/:site_id/groups/:group_id/tests/:id(.:format)         test_runs#destroy
    

    Update: Duh, I left the static segments of the scope paths out of my example. Fixed.

提交回复
热议问题