我们可以重命名资源 collection/member URL 吗?

Can we rename a resource collection/member URL?

我在 routes.rb

中有此资源
resources :orders, only:[] do
    collection do 
      post :user_order_confirmation
    end  
 end

它在 HTML 个视图 'orders/carrier_order_confirmation' 中创建了这样的路径。我们可以做一些让这条路径看起来像这样的事情吗 'orders/user_confirmation'?

您可以通过在设置资源路由后的routes.rb中添加以下行来覆盖常规路由:

get "/orders/user_confirmation" => "orders#user_confirmation", as: "user_confirmation"

为了澄清,下面是每个部分的含义:

[Routing Method] "/[Route You want]" => "[Controller]#[Controller Method]", as: "[Custom Route Name]"

试试这个 post :user_order_confirmation, 如:user_confirmation

你可以做到

resources :orders, only:[] do
    collection do 
      post :user_confirmation, acion: :user_order_confirmation
    end  
 end

这将为您提供所需的确切路线。这是测试:

$ rake routes | grep orders
user_confirmation_orders POST /orders/user_confirmation(.:format) orders#user_confirmation {:acion=>:user_order_confirmation}