0

我对 Rails (3.) 有点陌生,需要您的帮助才能使用重命名的路线。我有以下路线来展示和搜索产品。

  namespace :store do
    namespace :product do 
      resources :home
      resources :search
    end 
  end

rake routes为上述资源呈现以下输出:

  store_product_home_index GET    /store/product/home(.:format)                       store/product/home#index
                           POST   /store/product/home(.:format)                       store/product/home#create
    new_store_product_home GET    /store/product/home/new(.:format)                   store/product/home#new
   edit_store_product_home GET    /store/product/home/:id/edit(.:format)              store/product/home#edit
        store_product_home GET    /store/product/home/:id(.:format)                   store/product/home#show
                           PUT    /store/product/home/:id(.:format)                   store/product/home#update
                        DELETE    /store/product/home/:id(.:format)                   store/product/home#destroy
store_product_search_index GET    /store/product/search(.:format)                     store/product/search#index
                           POST   /store/product/search(.:format)                     store/product/search#create
  new_store_product_search GET    /store/product/search/new(.:format)                 store/product/search#new
 edit_store_product_search GET    /store/product/search/:id/edit(.:format)            store/product/search#edit
      store_product_search GET    /store/product/search/:id(.:format)                 store/product/search#show
                           PUT    /store/product/search/:id(.:format)                 store/product/search#update
                        DELETE    /store/product/search/:id(.:format)                 store/product/search#destroy

而不是像 /store/product/home 这样的路径,我想重命名为 /products/home。

所以修改后的路线应该如下所示:

  store_product_home_index GET    /products/home(.:format)                       store/product/home#index
                           POST   /products/home(.:format)                       store/product/home#create
    new_store_product_home GET    /products/home/new(.:format)                   store/product/home#new
   edit_store_product_home GET    /products/home/:id/edit(.:format)              store/product/home#edit
        store_product_home GET    /products/home/:id(.:format)                   store/product/home#show
                           PUT    /products/home/:id(.:format)                   store/product/home#update
                        DELETE    /products/home/:id(.:format)                   store/product/home#destroy
store_product_search_index GET    /products/search(.:format)                     store/product/search#index
                           POST   /products/search(.:format)                     store/product/search#create
  new_store_product_search GET    /products/search/new(.:format)                 store/product/search#new
 edit_store_product_search GET    /products/search/:id/edit(.:format)            store/product/search#edit
      store_product_search GET    /products/search/:id(.:format)                 store/product/search#show
                           PUT    /products/search/:id(.:format)                 store/product/search#update
                        DELETE    /products/search/:id(.:format)                 store/product/search#destroy

请注意,我使用的是 Rails 3.2.1。

非常感谢您提供的任何帮助。

4

1 回答 1

1

据我了解,您只想删除 /store 命名空间?

代替

 namespace :store do
    namespace :product do 
      resources :home
      resources :search
    end 
  end

namespace :product do 
  resources :home
  resources :search
end 

如果您出于结构原因想保留命名空间,请尝试

 namespace :store,:path => "" do
    namespace :product do 
      resources :home
      resources :search
    end 
  end

希望这可以帮助!

于 2012-05-13T16:59:26.360 回答