1

我正在尝试在我的根目录中实现 301 重定向,以删除重复的页面。但是,我还将根路径名从“产品”更改为“产品”。这是我的路线:

    resources :products, path: "produits" do
      resources :product_variants, only: [:new, :create, :edit, :update]

      collection do
        get :unavailable_products
      end
    end
    get '/products', to: redirect(path: '/produits')
    get '/products/:id', to: redirect('/produits/%{id}')

这是我的铁路路线:

 products GET    /products(.:format)                                                                      redirect(301, path: /produits)
                                      GET    /products/:id(.:format)                                                                  redirect(301, /produits/%{id})
             product_product_variants POST   /produits/:product_id/product_variants(.:format)                                         product_variants#create
          new_product_product_variant GET    /produits/:product_id/product_variants/new(.:format)                                     product_variants#new
         edit_product_product_variant GET    /produits/:product_id/product_variants/:id/edit(.:format)                                product_variants#edit
              product_product_variant PATCH  /produits/:product_id/product_variants/:id(.:format)                                     product_variants#update
                                      PUT    /produits/:product_id/product_variants/:id(.:format)                                     product_variants#update
        unavailable_products_products GET    /produits/unavailable_products(.:format)                                                 products#unavailable_products
                                      GET    /produits(.:format)                                                                      products#index
                                      POST   /produits(.:format)                                                                      products#create
                          new_product GET    /produits/new(.:format)                                                                  products#new
                         edit_product GET    /produits/:id/edit(.:format)                                                             products#edit
                              product GET    /produits/:id(.:format)                                                                  products#show
                                      PATCH  /produits/:id(.:format)                                                                  products#update
                                      PUT    /produits/:id(.:format)                                                                  products#update
                                      DELETE /produits/:id(.:format)                                                                  products#destroy

问题是我尝试将“get '/products', to: redirect(path: '/produits')”更改为“match”,但它给了我以下错误:

You should not use the `match` method in your router without specifying an HTTP method. If you want to expose your action to both GET and POST, add `via: [:get, :post]` option. If you want to expose your action to GET, use `get` in the router: Instead of: match "controller#action" Do: get "controller#action"

如何将所有产品页面重定向到“产品”并且没有“产品”的路线?

4

1 回答 1

0

我发现了问题,这是由于路线顺序造成的。

“get '/products', to: redirect(path: '/produits')”和其他必须在资源路由之后。

顺序很重要!

于 2020-10-09T13:13:52.133 回答