【问题标题】:How to name nested routes parameters in Rails?如何在 Rails 中命名嵌套路由参数?
【发布时间】:2018-01-15 13:40:57
【问题描述】:

我有以下嵌套路由:

resources :carts, only: [:show, :update, :create], param: :token do
  resources :items, :controller => :cartitems, except: [:new, :edit], param: :product_id
end

这会生成

GET    /api/merchants/:merchant_id/carts/:cart_token/items(.:format)             cartitems#index
POST   /api/merchants/:merchant_id/carts/:cart_token/items(.:format)             cartitems#create
GET    /api/merchants/:merchant_id/carts/:cart_token/items/:product_id(.:format) cartitems#show
PATCH  /api/merchants/:merchant_id/carts/:cart_token/items/:product_id(.:format) cartitems#update
PUT    /api/merchants/:merchant_id/carts/:cart_token/items/:product_id(.:format) cartitems#update
DELETE /api/merchants/:merchant_id/carts/:cart_token/items/:product_id(.:format) cartitems#destroy
...

我想删除resources :items参数中的控制器部分。这意味着:我想将:cart_token 参数重命名为:token。就像:

GET    /api/merchants/:merchant_id/carts/:token/items(.:format)             cartitems#index
POST   /api/merchants/:merchant_id/carts/:token/items(.:format)             cartitems#create
GET    /api/merchants/:merchant_id/carts/:token/items/:product_id(.:format) cartitems#show
PATCH  /api/merchants/:merchant_id/carts/:token/items/:product_id(.:format) cartitems#update
PUT    /api/merchants/:merchant_id/carts/:token/items/:product_id(.:format) cartitems#update
DELETE /api/merchants/:merchant_id/carts/:token/items/:product_id(.:format) cartitems#destroy
... 

如何做到这一点?

【问题讨论】:

  • 你能用你期望的路线列表编辑吗?

标签: ruby-on-rails rails-routing


【解决方案1】:

不知道这是否是最好的方法,但我得到了你期望的路线:

scope 'api/mechantes/:merchant_id/carts/:token' do
  resources :items, :controller => :cartitems, except: [:new, :edit], param: :product_id
end

希望这会有所帮助。祝你好运!

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 1970-01-01
    • 2015-03-19
    • 2011-11-07
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 2022-01-11
    • 2013-10-09
    相关资源
    最近更新 更多